[Haskell-beginners] fmap versus bind

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue May 3 04:22:02 CEST 2011


On Mon, May 2, 2011 at 11:14 PM, Antoine Latter <aslatter at gmail.com> wrote:
> 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.

And depending on the data type, 'f <$> m' may be more efficient than
'm >>= return . f'

> 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

Note that this should be "(: ds) <$> applyOp op x y".  I mean,

  (:) ds   ===   (ds :)
  flip (:) ds   ===   (\x -> x : ds)   ===   (: ds)

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

Applicative style is very nice, specially when your functions have
multiple arguments.  Consider

  op1 >>= \x1 -> op2 >>= \x2 -> return (f x1 x2)

  do {x1 <- op1
      x2 <- op2
      return (f x1 x2)}

against

  f <$> op1 <*> op2

Cheers,

-- 
Felipe.



More information about the Beginners mailing list