[Haskell-cafe] Cannot understand liftM2
durden at gmail.com
Mon Dec 11 12:03:41 EST 2006
Hi Cale !
On 12/11/06, Cale Gibbard <cgibbard at gmail.com> wrote:
> The monad instance which is being used here is the instance for ((->)
> e) -- that is, functions from a fixed type e form a monad.
> So in this case:
> liftM2 :: (a1 -> a2 -> r) -> (e -> a1) -> (e -> a2) -> (e -> r)
> I bet you can guess what this does just by contemplating the type. (If
> it's not automatic, then it's good exercise) Now, why does it do that?
> Well, in general,
> liftM2 f x y = do
> u <- x
> v <- y
> return (f u v)
> So, it runs each of the computations you give it to get parameters for
> f, and then returns the result of applying f to them.
> Let us know if you need more detail about anything here. I sort of
> skipped over some details in the presentation. (You might want to work
> out exactly what return and bind do in this monad in order to
> understand things completely -- you can work them out from the types
Your answer was very thorough and very clear. I am honestly overwhelmed :D.
Mind bending and rewarding.
Thank you very much.
I will be going through all your examples again slowly and with a
console on the side so that I am sure I grok as much as I can.
More information about the Haskell-Cafe