fmap :: Functor f => (a -> b) -> f a -> f b
fmap f (Just a)      = Just (f a)

We wrap Just around (f a) because f return a value with type b instead (Just b).

But in
(>>=) :: Monad m => m a -> (a -> m b) -> m b
Just x >>= f  = f x

We don't need to wrap Just around (f a) because f return (Just b).

--Trung



2012/12/20 Miguel Negrao <miguel.negrao-lists@friendlyvirus.org>

A 20/12/2012, às 14:07, Trung Quang Nguyen escreveu:

> 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?

That would be the implementation of fmap for Maybe:

instance  Functor Maybe  where
    fmap _ Nothing       = Nothing
    fmap f (Just a)      = Just (f a)

so, different behavior.

best,
Miguel
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners



--
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/