Assignment - Haskell Warmup

Due in Lecture March 3 (postponed from Feb 28).

To turn in your work, please put all of your functions, using the names specified in the homework, in a single file called 'homework6.hs'. Make a separate text file with the output of your tests with clear headings for the output from the different functions. Use the following turnin command to turn the file in electronically:

turnin homework6.hs homework6-output.txt
Do not zip or tar the files before turning them in. To check that the turnin was successful, use turnin -v

See the 341 homework submission guidelines for general information, and for tips about how to capture the output from Scheme to turn in.

For each Haskell function you define, also include an explicit type declaration in your program. You can either use the most general type, or restrict it appropriate. For example, for the double function in the lecture notes, if we left it up to Haskell it would have inferred the type Num a => a -> a, but we declared it as the more specific type Integer => Integer. Either of these declarations would have been OK for a question that asked you to write a double function.


  1. Write and test a Haskell function to find the area of a circle, given its radius. (Hint: the prelude defines the constant pi.)
  2. Write and test a recursive Haskell function rsquares that takes a list of numbers, and returns a new list of the squares of those numbers.
  3. Write and test a non-recursive version nsquares that takes a list of numbers, and returns a new list of the squares of those numbers. This non-recursive version should use map and lambda.
  4. Using the Tree datatype in the lecture notes, define and test a number of useful functions on trees:

    The height of a tree is defined as 0 for the empty tree, and otherwise 1 more than the maximum of the heights of the left and right children. If you need the definitions of inorder and postorder, look in any data structures book.

    As an example of treemap, if we define small as in the lecture notes, treemap double small should return (Node 8 EmptyTree (Node 10 EmptyTree EmptyTree)).

    For this question, you should use the following definition for trees:

    data Tree a = EmptyTree | Node a (Tree a) (Tree a)
                  deriving (Show,Read)
    
    The "deriving" part allows you to conveniently read and print trees.

    For this question, make your functions polymorphic and as general as possible in the tree arguments. (In other words, don't just find the heights of trees of booleans, find the heights of any kind of tree.)

  5. Write and test a Haskell function that uses infinite data structures in an interesting way. (Be creative!) If you can't think of anything interesting, you can do something uninteresting still for full credit. It also doesn't need to be really complex -- computing compound interest, or radioactive decay, or population growth, over an indefinite time, would all be OK.