[Haskell-beginners] About monad

Tom Davie tom.davie at gmail.com
Thu Dec 20 15:13:28 CET 2012


On 20 Dec 2012, at 14:07, Trung Quang Nguyen <trungnq97 at gmail.com> wrote:

> Hi all,
> 
> I saw this
> 
> instance Monad Maybe where  
>     return x = Just x  
>     Nothing >>= f = Nothing  
>     Just x >>= f  = f x  
>     fail _ = Nothing  
> 
> I am wondering about the implementation of function (>>=). Why don't it be Just x >>= f = Just (f x)?
> 
> Any body knows about this?

The reason is in the type of bind:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

The function f takes a non-in-a-monad value, and gives you an in-a-monad value.

Bob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/de0b1ecd/attachment.htm>


More information about the Beginners mailing list