> lookup :: Eq a => a -> [(a,b)] -> Maybe b > lookup k ((k',v):_) | k == k' = Just v > lookup k (_:xs) = lookup k xs > lookup k [] = Nothing
> lookup :: (Monad m, Eq a) => a -> [(a,b)] -> m b > lookup k ((k',v):_) | k == k' = return v > lookup k (_:xs) = lookup k xs > lookup k [] = fail "key not found"
- JohnMeacham
You could also use ContinuationPassingStyle, but UsingMonads is probably better.
Is there an argument for abstracting the failure behaviour out of Monad?
> class Failable m where > fail :: String -> m a > > class Monad m where > (>>=) :: m a -> (a -> m b) -> m b > return :: a -> m a
- AndrewBromage?
I've always felt a little disgusted by fail in the Monad class. (Also, firstly I confused it with mzero).
- StefanLjungstrand