CSE 130, Summer 2009

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 the type system, laziness, pattern matching, and purely functional programming (with curried functions) 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.)


Tasks

Part 1

Define the following functions:

All these functions should handle infinite sets and relations wherever possible. The tests provided contain some examples.

Part 2

Define the following functions:

Now, define the infinite list primes that contains all and only the prime numbers. Make sure to write some tests that convince you of this!

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:


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 testing.


Hints

In addition to the ideas and examples in Chapter 7 of PLAI you may find it useful to be familiar with ($), (++), all, elem, fst, snd, and show, and list comprehensions. If you import List you'll also have access to intersect, union, and nub. There's lots of other useful stuff in the Haskell prelude you can use without importing anything.


Submitting Your Work


Valid XHTML 1.0 Strict Valid CSS!