[ ^ CSE 341 | section index | <-- previous | next -->]

Exercise

Here are some concepts that you've (hopefully) seen before. How might you represent them using Miranda user-defined types?

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.


Last modified: Wed May 17 23:32:56 PDT 2000