Exercise
Here are some concepts that you've (hopefully) seen before. How might you represent them using Miranda user-defined types?
- quadratic: Quadratic polynomial described by the equation ax^2 + bx + c.
- pair: Scheme cons cell. (Can you make cons cells hold heterogeneous values?)
- binexp: Binary arithmetic expression tree; e.g., (* (+ 5 3) (/ 3 (+ 1 2))) can be represented as a tree with '*' at the root with '+' and '/' as its children, and so on, with literal numbers at the leaves.
- Define an eval_binexp function to evaluate binexp objects for addition and multiplication.
Abstract types
Abstract types simply dissociate the representation of a type from the operations that can be performed on that type (Note: clearly, this is a poor and incomplete implementation):
abstype heap * with emptyheap :: heap * isempty :: heap * -> bool deletemin :: heap * -> * add :: * -> heap * -> heap * heapnode * ::= Nil | HeapNode * (heapnode *) (heapnode *) heap * == heapnode * emptyheap = Nil isempty Nil = True isempty (HeapNode a b c) = False additem item Nil = (HeapNode item Nil Nil) additem item (HeapNode a b c) = (HeapNode (a) (additem item b) (c)) , if a < item = (HeapNode (item) (b) (additem a c)) , otherwise getmin Nil = error "getmin of empty heap" getmin (HeapNode a b c) = a I originally had a popmin function on this page, but it turns out that it's fairly hairy with 5 separate cases. However, you may want to try it at home. You can also try updating additem to make it more realistic.