
3 Aug
2004
3 Aug
'04
2:28 p.m.
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/