CIS 670, HOMEWORK ASSIGNMENT 1
Due Wednesday, September 14th
PRELIMINARIES
* If you're not already on the class mailing list, send me an email so I can
add you.
* If you don't own a copy of Types and Programming Languages, get one.
* Read Chapter 23 of TAPL (if you don't have your copy yet, I can send you a
PDF)
* Install OCaml on your favorite computer, if it isn't there already.
* Grab the file hw1.tar.gz from the course web site. Unpack it. From
inside the resulting directory, do:
make
./f test.f
Make sure you see some printed results of typechecking and evaluation.
HOMEWORK
* Define a less-than-or-equal function on church numerals -- i.e., a term of
type N -> N -> B that behaves as we'd expect it to. Add your function to
the end of test.f and check that it typechecks. Use "tobool" to write a
couple of tests, to make sure it works.
* Using the existing definition of List as a model, define a type Tree whose
elements represent binary trees whose internal nodes are labeled by church
numerals.
* Write functions "leaf" and "node" for constructing binary trees, by
analogy with nil and cons.
* Write a function "keyof" that returns the number on the root node of a
binary tree, by analogy with head.
* Write functions "leftchild" and "rightchild", by analogy with tail. Use
keyof and tonat to test what you've done.
* Write a function "insert" that takes a binary tree and a number and,
assuming the input tree is a well-formed binary search tree (i.e., the key
on every node is at least as large than any of the keys in its left
subtree and strictly smaller than any of the keys in its right subtree),
inserts the number into the tree, maintaining the same invariant.
Again, use keyof and tonat to test what you've done.
* OPTIONAL CHALLENGE PROBLEM: How about deletion from binary search trees?