[Haskell-cafe] exceptions vs. Either

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Tue Aug 3 10:08:00 EDT 2004

> Exceptions should only really be used for unpredictcable events, I find
> that the defintion of functions like head is lacking rigor... I would 
> prefer to see:
> head :: [a] -> Maybe a
> head (a0:_) = Just a0
> head _ = Nothing

In principle, yes, but in practice, that would be silly.  You use
"head" just when you know for sure that the list is non-empty; if it
is not, it's a "program error" for head, and an "impossible error" for
the caller.  Consider:

f (head xs)  -- old style


f (case head xs of Some x -> x; None -> error "whoops")  -- Schupke style

It should be clear that this function would never be used - if head
had this signature, programmers would just write

f (case xs of (x:_) -> x; [] -> error "whoops")  -- direct style

--KW 8-)

More information about the Haskell-Cafe mailing list