Exceptions
ML has functions hd and t1 to return the head (or ?rst element) and tail (or remaining elements) of a list. These both raise an exception Empty if the list is empty. Suppose that we rede?ne these functions without changing their behavior on nonempty lists, so that hd raises exception Hd and tl raises exception Tl if applied to the empty list nil:
- hd(nil); uncaught exception Hd - tl(nil); uncaught exception Tl
Consider the function
fun g(l) = hd(l) : : tl(l) handle Hd => nil; that behaves like the identity function on lists. The result of evaluating g(nil) is nil. Explain why. What makes the function g return properly without handling the exception Tl?