Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cartesian product of 2 lists in Haskell

I wish to produce the Cartesian product of 2 lists in Haskell, but I cannot work out how to do it. The cartesian product gives all combinations of the list elements:

xs = [1,2,3] ys = [4,5,6]  cartProd :: [a] -> [b] -> [(a,b)] cartProd xs ys ==> [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)] 

This is not an actual homework question and is not related to any such question, but the way in which this problem is solved may help with one I am stuck on.

like image 336
Callum Rogers Avatar asked Nov 07 '10 21:11

Callum Rogers


People also ask

What are list comprehensions in Haskell?

List comprehension in Haskell is a way to produce the list of new elements from the generator we have passed inside it. Also for the generator values, we can apply the Haskell functions to modify it later. This list comprehension is very y easy to use and handle for developers and beginners as well.

Is Cartesian product the same as cross product?

It's just the same symbol, and definitely not the same thing: the Cartesian product is a set (of vectors), the cross product is a vector.

What is cons operator in Haskell?

The : operator is known as the "cons" operator and is used to prepend a head element to a list. So [] is a list and x:[] is prepending x to the empty list making a the list [x] . If you then cons y:[x] you end up with the list [y, x] which is the same as y:x:[] .


2 Answers

This is very easy with list comprehensions. To get the cartesian product of the lists xs and ys, we just need to take the tuple (x,y) for each element x in xs and each element y in ys.

This gives us the following list comprehension:

cartProd xs ys = [(x,y) | x <- xs, y <- ys] 
like image 93
sepp2k Avatar answered Sep 22 '22 14:09

sepp2k


As other answers have noted, using a list comprehension is the most natural way to do this in Haskell.

If you're learning Haskell and want to work on developing intuitions about type classes like Monad, however, it's a fun exercise to figure out why this slightly shorter definition is equivalent:

import Control.Monad (liftM2)  cartProd :: [a] -> [b] -> [(a, b)] cartProd = liftM2 (,) 

You probably wouldn't ever want to write this in real code, but the basic idea is something you'll see in Haskell all the time: we're using liftM2 to lift the non-monadic function (,) into a monad—in this case specifically the list monad.

If this doesn't make any sense or isn't useful, forget it—it's just another way to look at the problem.

like image 38
Travis Brown Avatar answered Sep 19 '22 14:09

Travis Brown