[Haskell-cafe] Re: Request to review my attempt at understanding Monads

Maciej Piechotka uzytkownik2 at gmail.com
Tue Dec 29 06:41:37 EST 2009


On Tue, 2009-12-29 at 02:07 -0800, CK Kashyap wrote:
> Thanks Jason,
> 
> 
> > 
> >   You should make a `Functor' instance since monads are all
> >   functors (though the typeclass does not enforce this).
> > 
> What are the benefits of making it an instance of Functor?
> 

1. For example to use function of type Functor f => f a -> f d.

2. Also you need Functor to have Applicative which is rather useful (f <
$> arg1 <*> arg2 <*> arg3 <*> ... as opposed to return f `ap` arg1 `ap`
arg2 `ap` arg3 ..., (*>), (<*) etc.)

3. Because it is functor ;). Every Monad is functor:

instance Functor MyMonad where
  fmap  = liftM
instance Applicative MyMonad where
  pure  = return
  (<*>) = ap

4. If you use Control.Applicative you can find:
read <$> getLine
I find it much more readable then liftM read getLine (it looks nearly
like read $ getLine).

Regards




More information about the Haskell-Cafe mailing list