[Haskell-beginners] fmap versus bind

Sean Perry shaleh at speakeasy.net
Tue May 3 22:09:11 CEST 2011


On Tue May  3  8:50 , Felipe Almeida Lessa  sent:

>On Tue, May 3, 2011 at 12:32 PM, Patrick LeBoutillier
>patrick.leboutillier at gmail.com> wrote:
>> I'm trying to understand this example and I can't get the types to line up.
>> Can you provide the "real" types for ds and (applyOp op x y)?
>>
>> This is what I'm working out:
>>
>>     fmap ((:) ds) (applyOp op x y)
>> == fmap (ds :) (applyOp op x y)
>> == fmap (\dss -> ds : dss) (applyOp op x y)
>>
>>      (applyOp op x y) >>= (flip (:) ds)
>> == (applyOp op x y) >>= (: ds)
>> == (applyOp op x y) >>= (\d -> d : ds)
>>
>> To my untrained eyes is doesn't even look like the code is doing the
>> same thing...
>> What am I missing here (or more probably where is my mistake)?
>
>I think that OP forgot a return, i.e., he meant 'applyOp op x y >>=
>return . flip (:) ds'.
>

You are correct, I did forget the return. Sorry for the confusion.

This particular bit of code was pulled out of the middle of a RPN calculator. So
'ds' is a list of doubles. applyOp takes 2 numbers and emits a 'm double' which
we then want to extract and add to the top of the list. The 'm' is there because
I do my calculation in the Either monad to support unknown operators, invalid
numbers, etc. A left value in the bind call stops the execution.

To my still novice eyes 'foo >>= handleResult' is more readable than 'fmap
handleResult foo'. When I look at the fmap call it looks like handlResult is the
more important computation.




More information about the Beginners mailing list