All combinations of elements of two lists in Haskell

Ben picture Ben · Aug 19, 2015 · Viewed 11.3k times · Source

Given two lists, [a, b] and [c, d], I'd like to get the following result:

[(a,c), (a,d), (b,c), (b,d)]

How can I do this in Haskell? Is there a built-in function for this, or should I implement one myself?

Answer

leftaroundabout picture leftaroundabout · Aug 19, 2015
[ (x,y) | x<-[a,b], y<-[c,d] ]

This doesn't really require any further explanation, does it?