[Haskell-beginners] fmap versus bind

Antoine Latter aslatter at gmail.com
Tue May 3 04:14:13 CEST 2011


On Mon, May 2, 2011 at 8:56 PM, Sean Perry <shaleh at speakeasy.net> wrote:
> i came across hlint today and ran it on my experiments. It pointed out places where I was overly cautious with parens or used a $ and forget to remove it later. But one suggestion it made I was curious about.
>
> Why does it recommend using fmap over >>=? Idiomatically does fmap make more sense when the Monad is more like a collection?
>
> For instance I had "(applyOp op x y) >>= (flip (:) ds)". This seems more clear than "fmap ((:) ds) (applyOp op x y)".

Part of the reason for the suggestion is that bind is "more powerful"
than fmap, which means that it asks more of the implementing type,
which then means that fewer type satisfy bind than satisfy fmap. So
your function is more general when written using fmap.

I might write this with the infix version of fmap, but some folks
don't like the explosion of operators:

> ((:) ds) <$> applyOp op x y

or closer to yours:

> flip (:) ds <$> applyOp op x y

If the type of the function is already fixed I think it comes down to
personal preference.

Antoine

> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list