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@gmail.com>

On 20 Dec 2012, at 14:07, Trung Quang Nguyen <trungnq97@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 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/