[Haskell] modern language design, stone age tools
MR K P SCHUPKE
k.schupke at imperial.ac.uk
Wed Jun 23 15:56:15 EDT 2004
I like to write programs so functions cannot fail...
so head should really be:
maybeHead :: [a] -> Maybe a
maybeHead (a:_) = Just a
maybeHead _ = Nothing
etc...
The the calling function can do something like:
case maybeHead x of
Just y -> <expr y>
Nothing -> fail "failed in..."
This way you can pass the failure back up to somewhere where
its meaningful.
In my opinion you should either be encoding failure in the
return type, or using exceptions... anything else is just
bad coding.
Finally use guards (that can be conditionally compiled out)
before functions that might fail:
if null x then fail "sesible error message" else ...
Keean.
More information about the Haskell
mailing list