|
|
|
|
|
by steveklabnik
3807 days ago
|
|
The usual implementation of the Either/Option monads already does this though: instance Monad Maybe where
return x = Just x
Nothing >>= f = Nothing
Just x >>= f = f x
fail _ = Nothing
and instance (Error e) => Monad (Either e) where
return x = Right x
Right x >>= f = f x
Left err >>= f = Left err
fail msg = Left (strMsg msg)
The end result is still a value of that type. You get the short-circuting behavior as soon as you hit Nothing/Left. |
|