CSE 130, Summer Session I, 2008

Programming Languages: Principles and Paradigms

Programming Assignment 4


Starter code (and tests): pa4.hs

The purpose of this assignment is to get some hands-on experience with laziness, the type system, and pattern matching in Haskell. It is to be completed individually according to the course collaboration policy.

You can work on this assignment using GHCi on ACS machines or on your own machine. (GHCi is the interactive interpreter provided with the Glasgow Haskell Compiler.) On an ACS GNU/Linux machine you will need to run prep cs130u before you can run ghci. You can cause GHCi to load a file by passing it a file name on the command line. You can also load a file using :l file, in this case leaving off the .hs file extension.

Note that if any error occurs while loading a file, GHCi will load nothing from the file. For this reason it may come in handy to comment out large blocks using {- the delimiters surrounding this phrase -}.


Tasks

Part 1

Using Haskell's pattern matching, define the following functions, whose type declarations are provided for you in the starter code:

Hint: You may want to look at Haskell's div function.

Part 2

Define the following functions, whose type declarations are provided for you in the starter code:

Now, define the infinite list primes that contains all and only the prime numbers. Make sure to define some tests that convince you it contains what it should!

Part 3

Examine the BinaryTree type, which has two variants: Branch and Leaf. Note the type variables a for values in branches, and b for those in leaves. So the type is polymorphic, but we will use a BinaryTree as an abstract syntax tree representing a simple arithmetic expression on integers, so a will be a BinOp type and b will be a Num.

Now define the following functions, whose type declarations are provided for you:

Hints: You may want to look at Haskell's show function and (++) operator, and refer to Chapter 7 of PLAI for examples of functions that operate on defined types.


Testing

As with previous assignments, you will need to think carefully about where your code might fail and add your own test cases to the test file. Your submission will be tested against a more comprehensive suite of tests than we gave you and we may also assess the quality of your tests.


Submitting Your Work


Valid XHTML 1.0 Strict Valid CSS!