title: Cartesian (outer) product of lists in OCaml
body: <p>I would like to iterate over all combinations of elements from a list of lists which have the same length but not necessarily the same type. This is like the cartesian product of two lists (which is easy to do in OCaml), but for an arbitrary number of lists.</p>
  
  <p>First I tried to write a general cartesian (outer) product function which takes a list of lists and returns a list of tuples, but that can't work because the input list of lists would not have elements of the same type. </p>
  
  <p>Now I'm down to a function of the type</p>
  
  <pre><code>'a list * 'b list * 'c list -&gt; ('a * 'b * 'c) list
  </code></pre>
  
  <p>which unfortunately fixes the number of inputs to three (for example). It's</p>
  
  <pre><code>let outer3 (l1, l2, l3) =
     let open List in
     l1 |&gt; map (fun e1 -&gt; 
         l2 |&gt; map (fun e2 -&gt;
             l3 |&gt; map (fun e3 -&gt;
                 (e1,e2,e3))))
     |&gt; concat |&gt; concat
  </code></pre>
  
  <p>This works but it's cumbersome since it has to be redone for each number of inputs. Is there a better way to do this?</p>
  
  <p>Background: I want to feed the resulting flat list to <code>Parmap.pariter</code>.</p>