
On Sat, Sep 12, 2009 at 09:13:58PM +0300, Michael Snoyman wrote:
I often times have to write a lookup function that returns its value into any monad instead of just Maybe. For example:
mLookup :: (Eq k, Monad m) => k -> [(k, v)] -> m v mLookup k pairs = case lookup k pairs of Nothing -> fail "mLookup: nothing found" Just v -> return v
This is actually the type that the lookup function USED to have, but it was changed since monads actually have nothing to do with failing (the fail method is just a hack used to handle pattern-match failures in do-notation). Probably a better implementation of this would be mLookup :: (Eq k, MonadPlus m) => k -> [(k,v)] -> m v mLookup k pairs = maybe mzero return (lookup k pairs) -Brent