[Haskell-cafe] exceptions vs. Either

Graham Klyne GK at ninebynine.org
Tue Aug 3 13:21:07 EDT 2004

At 15:28 03/08/04 +0100, MR K P SCHUPKE wrote:
> >f (case xs of (x:_) -> x; [] -> error "whoops")  -- direct style
>Yup, this is how I do it... I never use head!

As a general principle, this bothers me.

In the longer term (i.e. if and when large-scale production Haskell systems 
become common), and as a matter of principle, I think it's better to use a 
prelude (or standard) function when one will do the job, because a 
widely-used "industrial strength" compiler might well have special 
knowledge of these and be able to apply special optimizations (as, e.g., 
some C/C++ compilers do for standard library functions like memcpy).

As for head, I think it's fine that it throws an error because it is 
specified to be defined for only non-empty lists.  (I remain silent on 
whether the prelude should contain a head-like function that returns a 
value for empty lists.)


Graham Klyne
For email:

More information about the Haskell-Cafe mailing list