[email protected]
[Top] [All Lists]

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

Subject: [Haskell-cafe] Re: Request to review my attempt at understanding Monads
From: Maciej Piechotka
Date: Tue, 29 Dec 2009 12:41:37 +0100
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


_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

<Prev in Thread] Current Thread [Next in Thread>