CS321 Prog Lang & Compilers           Assignment # 2
Assigned: Jan 10, 2007      Due: Wed  . Jan 17, 2007

Using the primitive types (int, boolean, etc) and the data structures defined in the
notes for the second lecture. Tree etc. Include your program file, and a listing where
you test your functions. You must include 3 tests for each function.

1) write a function which computes n factorial. E.g.
   fact 0  --> 1     fact 3  --> 6    fact 5 --> 120

(You will need to use recursion.)

2) write even and odd functions.
   e.g.  even 4 --> true
         odd 10 --> false
         odd 3 --> true

(Hint the infix function "mod" is useful.  6 mod 3  --> 0,  4 mod 3 --> 1)

3) write the ncopies function. For example:
   ncopies 3 5     --> [5,5,5]
   ncopies 4 "a"   --> ["a","a","a","a"]
   ncopies 0 true  --> []

(You will need an ‘if’ expression and recursion)

4) Use pattern matching to write a function to compute the depth of a Tree

(you will need to include the datatype declaration for Tree in your file. You will
need 2 equations. You will need to use pattern matching. You will need recursion.)

5) Can you think of a function over trees? Explaing what it does in a comment, and
then write it in ML.




Back to CS 321, Languages and Compiler Design, Assignment Page