Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All combinations of elements of two lists in Haskell

Tags:

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?

like image 282
Ben Avatar asked Aug 19 '15 11:08

Ben


People also ask

What does ++ mean in Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combine" them into a single list.

How do I create a pair in Haskell?

Use parentheses and commas to create tuples. Use one comma to create a pair. Use more commas to create tuples with more components. Note that it is also possible to declare tuples using in their unsugared form.

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.


1 Answers

[ (x,y) | x<-[a,b], y<-[c,d] ]

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

like image 153
leftaroundabout Avatar answered Sep 22 '22 05:09

leftaroundabout