[Haskell-cafe] exceptions vs. Either

MR K P SCHUPKE k.schupke at imperial.ac.uk
Tue Aug 3 10:28:19 EDT 2004


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

Yup, this is how I do it... I never use head!

I like to pass failures back up to the level where some kind of sensible 
error message can be generated. In your example the error is no
better than with 'head' - the point is a Nothing can be 'caught' 
outside of an IO monad. 

I would suggest using the type system as I said earlier so:

toNonEmptyList :: [a] -> Maybe (NonEmpty a)
toNonEmptyList (a0:_) = Just (NonEmpty a)
toNonEmptyList _ = Nothing

Then redefine head:

head :: NonEmpty a -> a
head (NonEmpty (a0:_)) = a0


	Keean/


More information about the Haskell-Cafe mailing list