Reputation: 9909
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)
Upvotes: 4
Views: 93
Reputation: 153
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.
Upvotes: 9