2

I am trying to write something like this

[(x,y)|x<- [1,2,3], y <- [’a’,’b’]]
 => [(1,’a’),(1,’b’),(2,’a’),(2,’b’),(3,’a’),(3,’b’)]
unj2
  • 52,135
  • 87
  • 247
  • 375
  • possible duplicate: http://stackoverflow.com/questions/935996/calculating-the-cartesian-product-of-a-list-of-numbers-with-f – BrokenGlass Mar 17 '11 at 03:36

3 Answers3

5
[for x in [1;2;3] do
 for y in ['a';'b'] do
 yield x,y]
kvb
  • 54,864
  • 2
  • 91
  • 133
1

just another fun way

[1;2;3] |> List.map ( fun X -> ['a';'b'] |> List.map (fun A -> X,A) )
cnd
  • 32,616
  • 62
  • 183
  • 313
-2

F# equivalent.

List.zip [1;2;3] ['a';'b';'c']
Nyi Nyi
  • 678
  • 5
  • 13
  • 1
    Not quite - this pairs up elements in corresponding positions (and therefore requires lists of the same length); the question is looking for the Cartesian product of the two lists. – kvb Mar 17 '11 at 03:37
  • @kvb - Thanks for pointing out. I need to be more careful about comprehending question next time. – Nyi Nyi Mar 17 '11 at 04:47