Homework 7 — Programming Language Paradigms

Objective

More practice with function definitions, pattern matching, and recursion in Haskell.

Due Date

Beginning of class session 17.

Tasks

  1. Your programming for this assignment must be done in the folder HaskellHomework within your local copy of your SVN repository.
  2. Using your favorite text editor, create a file basics.hs within the HaskellHomework folder. You must use exactly that file name or I'll deduct 50% from your homework score. This is so we can efficiently grade your and your classmates' work.
    1. Write a single-line recursive definition of a function multiples :: Int -> [Int] that returns an infinite list of all the non-negative multiples of its argument. Include tests for your function. You may use prelude functions, like map.

      For example:

      Main> take 10 (multiples 5)
      [0,5,10,15,20,25,30,35,40,45]
      

      Here's a non-recursive solution that is probably better, but I want you to use recursion:

      betterMultiples :: Int -> [Int]
      betterMultiples n = [0,n..]
      
  3. Do an SVN update on the folder HaskellHomework within your local copy of your SVN repository. You should see a file Trees.hs. Your programming for this task must be done in that file.

    The Trees.hs file includes the declaration of a Tree datatype. Implement and test the tree traversals below. (A complete test IO action is provided; you’ll need to uncomment tests as you write your answers.)

    1. preorder :: BinaryTree a -> [a], which takes a tree and returns its pre-order traversal.
    2. inorder :: BinaryTree a -> [a], which takes a tree and returns its in-order traversal.
    3. postorder :: BinaryTree a -> [a], which takes a tree and returns its post-order traversal.
    4. Bonus: levelorder :: BinaryTree a -> [a], which takes a tree and returns its level-order traversal.

Turn-in Instructions

Turn in your work by committing it to your SVN repository.