CIS-3030 Homework #1: Simple Functions

Due: Wednesday, August 28, 2013

Read the Starting Out chapter in the text. Spend some time browsing around the Haskell web site to get familiar with what is available there. In the book Real World Haskell you could also look over chapter 1 and chapter 2. This reading can spill over into next week.

For this assignment you are to write several functions as described below. Put your work in a file named homework1.hs. Include a comment block at the top that has at least your name. Also indicate which question each function is answering using comments. Be sure your file loads in ghci successfully.

  1. The "standing wave ratio" on a transmission line is a measure of how well matched an antenna is to the cable that feeds it. If Za is the impedance of the antenna and Z0 is the "characteristic impedance" of the transmission line, then SWR = (1 + |rho|)/(1 - |rho|) where rho = (Za - Z0)/(Za + Z0) is the reflection coefficient. Write a Haskell function that computes standing wave ratio given Za and Z0. What is the SWR if a 300 ohm antenna is connected to 50 ohm coaxial cable? (The ideal SWR is 1.0; larger values are worse). Hint: Haskell's absolute value function is named abs.

  2. Write a function rotateLeft that rotates a list one position to the left. For example given a list such as [1, 2, 3, 4] it should return [2, 3, 4, 1]. Hint: append the head of the list onto its tail. What happens when you apply your function to the string "Hello?"

  3. Write a function rotateLeft2 that rotates a list by two positions to the left. For example given a list such as [1, 2, 3, 4] it should return [3, 4, 1, 2]. Hint: Use drop and take. Is there a way to use rotateLeft from the previous question to implement rotateLeft2?

  4. Generalize your rotation function by writing a function rotateLeftN that rotates a list by n positions (where n is a parameter of the function). What happens if you use a value of n that is larger than the length of the list? Is it the effect you would expect? Can you see an easy way to get the effect you expect? Hint: Think about using the mod function.

  5. Write a function basicMath that takes two numbers and returns a 4-tuple consisting of the sum, difference, product, and quotient of the numbers. For example, given 8 and 2 it should return (10, 6, 16, 4).

  6. Write a function makeLists that takes two values and returns a pair of lists that has those two values in either order. For example, given 1 and 2 it should return ([1, 2], [2, 1]). What happens when you apply makeLists to the values 1 and "Hello?"

  7. Write a function factors that takes an integer n and returns a list of all factors of n. For example, given 12 the function should return [1, 2, 3, 4, 6, 12]. A factor of n is a number that divides into n evenly. Hint: Use a list comprehension and the mod function.

  8. The following function isPrime returns True if given an integer n that is prime. It uses techniques we haven't discussed yet. Use this function to generate a list of all prime numbers up to 100. Hint: Use a list comprehension with isPrime as the predicate.

            isPrime n = all (\x -> mod n x /= 0) [ 2 .. (n - 1) ]
    
  9. Combine the ideas from the previous two answers and write a function primeFactors that takes an integer n and returns a list of all the prime factors of n.


Last Revised: 2013-08-15
© Copyright 2013 by Peter C. Chapin <PChapin@vtc.vsc.edu>