Haskell Language Design Questions
Tom Pledger
Tom.Pledger@peace.com
Tue, 9 Jan 2001 11:04:38 +1300
Doug Ransom writes:
[...]
> 2. It seems to me that the Maybe monad is a poor substitute for
> exception handling because the functions that raise errors may not
> necessarily support it.
It sometimes helps to write such functions for monads in general,
rather than for Maybe in particular. Here's an example adapted from
the standard List module:
findIndex :: (a -> Bool) -> [a] -> Maybe Int
findIndex p xs = case findIndices p xs of
(i:_) -> Just i
[] -> Nothing
It generalises to this:
findIndex :: Monad m => (a -> Bool) -> [a] -> m Int
findIndex p xs = case findIndices p xs of
(i:_) -> return i
[] -> fail "findIndex: no match"
The price of the generalisation is that you may need to add some type
signatures to resolve any new overloading at the top level of the
module.
Regards,
Tom