[Haskell-beginners] About monad

Trung Quang Nguyen trungnq97 at gmail.com
Thu Dec 20 15:25:45 CET 2012


Oh yes, I understand now.

Just x >>= f  = f x
the output of f is actually (Monad value) like in this example

(Just 3) >>= (\x -> Just $ x^2)

At the first sight, I thought about (Monad (f x)), but it's wrong because
it will be (Monad (Monad value)) when f return.

Thanks a lot!

--Trung




2012/12/20 Tom Davie <tom.davie at gmail.com>

>
> On 20 Dec 2012, at 14:07, Trung Quang Nguyen <trungnq97 at gmail.com> wrote:
>
> Hi all,
>
> I saw this
>
>
>    1. instance Monad Maybe where
>    2.     return x = Just x
>    3.     Nothing >>= f = Nothing
>    4.     Just x >>= f  = f x
>    5.     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
>
>


-- 
*Trung Nguyen*
Mobile: +45 50 11 10 63
LinkedIn: http://www.linkedin.com/pub/trung-nguyen/36/a44/187
View my blog at http://www.onextrabit.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/fbbf70e2/attachment-0001.htm>


More information about the Beginners mailing list