[Haskell-cafe] Cannot understand liftM2

John Meacham john at repetae.net
Thu Feb 9 20:23:54 CET 2012


A good first step would be understanding how the other entry works:

cartProd :: [a] -> [b] -> [(a,b)]
cartProd xs ys = do
        x <- xs
        y <- ys
        return (x,y)

It is about halfway between the two choices.

    John

On Thu, Feb 9, 2012 at 9:37 AM, readams <richard.adams at lvvwd.com> wrote:
> Nice explanation.  However, at
> http://stackoverflow.com/questions/4119730/cartesian-product it was pointed
> out that this
>
> cartProd :: [a] -> [b] -> [(a, b)]
> cartProd = liftM2 (,)
>
> is equivalent to the cartesian product produced using a list comprehension:
>
> cartProd xs ys = [(x,y) | x <- xs, y <- ys]
>
> I do not see how your method of explanation can be used to explain this
> equivalence?  Nevertheless, can you help me to understand how liftM2 (,)
> achieves the cartesian product?  For example,
>
> Prelude Control.Monad.Reader> liftM2 (,) [1,2] [3,4,5]
> [(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)]
>
> Thank you!
>
> --
> View this message in context: http://haskell.1045720.n5.nabble.com/Cannot-understand-liftM2-tp3085649p5470185.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list