It might be helpful to fully replace the >>= operators with their definitions:<br><br>m1 >>= \x1 -> m2 >>= \x2 -> return (f x1 x2)<br>foldr ((++) . (\x1 -> m2 >>= \x2 -> return (f x1 x2))) [] m1<br>
foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> return (f x1 x2))) [] m2)) [] m1<br>foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [f x1 x2])) [] m2)) [] m1<br><br>which in your example yields:<br><br>foldr ((++) . (\x1 -> foldr ((++) . (\x2 -> [x1 + x2])) [] [3,4])) [] [1,2]<br>
<br>Does that make a bit more sense?<br><br clear="all">-R. Kyle Murphy<br>--<br>Curiosity was framed, Ignorance killed the cat.<br>
<br><br><div class="gmail_quote">On Fri, Feb 24, 2012 at 11:42, <span dir="ltr"><<a href="mailto:franco00@gmx.com">franco00@gmx.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span style="font-family:Verdana"><span style="font-size:12px"><div>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">I am a bit puzzled by liftM2. I know what it does, but let's take a look at<br>
                the implementation.</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r<br>
                liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">Which could be rewritten (I am a bit uncomfortable with the do notation):</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">m1 >>= \x1 -><br>
                m2 >>= \x2 -><br>
                return (f x1 x2)</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">so: liftM2 (+) [1,2] [3,4] = [4,5,5,6]</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">The instance of the List monad tells me that:</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">instance Monad [] where<br>
                 m >>= k = foldr ((++) . k) [] m<br>
                 m >> k = foldr ((++) . (\ _ -> k)) [] m<br>
                 return x = [x]<br>
                 fail _ = []</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">That foldr is just doing a concatMap, it seems to me.<br>
                But I still don't get the | return (f x1 x2) | part.<br>
                The first thing I thought was:</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">return ( (+) [1,2] [3,4] )</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">but of course this is not the IO monad, the bind operator does not just<br>
                fetch stuff from outer space.<br>
                 <br>
                What is there really 'inside' x1 and x2 when the return statement is<br>
                evaluated, though?</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">I think I intuitively got it (x1 and x2 are bound to m1 and m2, every<br>
                f x1 x2 expression is evaluated and then put together in a list), but<br>
                it would be helpful to see what really happens to interiorise it.</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">I started from the first step</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">(>>=) m1 = foldr. ((++) . k) [] m1 -- partially applying m1</span></span></p>
        <p style="margin:0px;padding:0px">
                <span style="font-family:Verdana"><span style="font-size:12px">but cannot really proceed from there. Any help will be appreciated,<br>
                if you think the question is not clear enough, do not hesitate to ask.</span></span></p>
</div>
</span></span>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br>