title: How do I compute the cartesian product of n sequences in F#?
body: <p>I was given a puzzle as a present. It consists of 4 cubes, arranged side by side. The faces of each cube are one of four colours.</p>
  
  <p>To solve the puzzle, the cubes must be orientated so that all four cubes' tops are different, all their fronts are different, all their backs are different and all their bottom's are different. The left and right sides do not matter.</p>
  
  <p>My pseudo-code solution was:</p>
  
  <ol>
  <li>Create a representation of each
  cube.</li>
  <li>Get all the possible orientations of
  each cube (there are 24 for each).</li>
  <li>Get all the possible combinations of
  orientations of each cube.</li>
  <li>Find the combination of orientations
  that satisfies the solution.</li>
  </ol>
  
  <p>I solved the puzzle using an implementation of that pseudo-code in F#, but am not satisifed with the way I did step 3:</p>
  
  <pre><code>let problemSpace =
      seq { for c1 in cube1Orientations do
                for c2 in cube2Orientations do
                    for c3 in cube3Orientations do
                        for c4 in cube4Orientations do
                            yield [c1; c2; c3; c4] }
  </code></pre>
  
  <p>The above code is very concrete, and only works out the cartesian product of four sequences of orientations. I started thinking about a way to write it for n sequences of orientations.</p>
  
  <p>I came up with (all the code from now on should execute fine in F# interactive):</p>
  
  <pre><code>// Used to just print the contents of a list.
  let print = 
      Seq.fold (fun s i -&gt; s + i.ToString()) "" &gt;&gt; printfn "%s"
  
  // Computes the product of two sequences - kind of; the 2nd argument is weird.
  let product (seq1:'a seq) (seq2:'a seq seq) =
      seq { for item1 in seq1 do
                for item2 in seq2 do
                    yield item1 |&gt; Seq.singleton |&gt; Seq.append item2 }
  </code></pre>
  
  <p>The product function could be used like so...</p>
  
  <pre><code>seq { yield Seq.empty }
  |&gt; product [ 'a'; 'b'; 'c' ]
  |&gt; product [ 'd'; 'e'; 'f' ]
  |&gt; product [ 'h'; 'i'; 'j' ]
  |&gt; Seq.iter print
  </code></pre>
  
  <p>... which lead to ...</p>
  
  <pre><code>let productn (s:seq&lt;#seq&lt;'a&gt;&gt;) =
      s |&gt; Seq.fold (fun r s -&gt; r |&gt; product s) (seq { yield Seq.empty })
  
  [ [ 'a'; 'b'; 'c' ]
    [ 'd'; 'e'; 'f' ]
    [ 'h'; 'i'; 'j' ] ]
  |&gt; productn
  |&gt; Seq.iter print
  </code></pre>
  
  <p>This is exactly the usage I want. productn has exactly the signature I want and works.</p>
  
  <p>However, using product involves the nasty line seq { yield Seq.empty }, and it unintuitively takes:</p>
  
  <ol>
  <li>A sequence of values (seq&lt;'a&gt;)</li>
  <li>A sequence <em>of sequences</em> of values (seq&lt;seq&lt;'a&gt;&gt;)</li>
  </ol>
  
  <p>The second argument doesn't seem correct.</p>
  
  <p>That strange interface is hidden nicely by productn, but is still nagging me regardless.</p>
  
  <p>Are there any nicer, more intuitive ways to generically compute the cartesian product of n sequences? Are there any built in functions (or combination of) that do this?</p>