September 22, 1999 Strong Typing Slide #22

Final Note About Union Types

        datatype 'a Tree = Nil
                         | Node of ('a * ('a Tree) * ('a Tree));
        fun num_nodes Nil = 0
          | num_nodes (Node(_, left, right)) = 
                1 + num_nodes(left) + num_nodes(right);
                val num_nodes = fn : 'a Tree -> int
        fun node_sum Nil = 0
          | node_sum (Node(n, left, right)) =
                n + node_sum(left) + node_sum(right);
                val node_sum = fn : int tree -> int

Type checker can detect omissions in complicated case analyses


Next Copyright © 1999 M-J. Dominus