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 HaskellTrees within your local copy of your SVN repository. Do an SVN update to grab that folder.
  2. In the HaskellTrees folder, you should see a file basics.hs. Your programming for this task must be done in that file. 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. In HaskellTrees you should also 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.
  4. From the command line, we can use commands like runghc Trees.hs to execute a Haskell file like a script. Without additional arguments this command would execute the main function of Trees.hs.

    Make sure the runghc Trees.hs executes the tests for your Trees solution and runghc basics.hs executes your multiples tests.

Turn-in Instructions

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