The following is an example from a Haskell tutorial
instance Monad Maybe where
return x = Just x
Nothing >>= f = Nothing
Just x >>= f = f x
fail _ = Nothing
However, I am confused by the Just x...
line. Should the result not be a monad? I would expect the line to be
Just x >>= f = Just (f x)
Yes! You're right about the result being a monad, but remember the type of the >>=
operator: m a -> (a -> m b) -> m b
. Then, we guess that f
has type a -> m b
, so applying f
to x
returns a monad as a result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With