title: Generalize list combinations to N lists
body: <p>Generating the combination of a known number of lists is quite simple in Scala. You can either use a for-comprehension:</p>
  
  <pre><code>for {
     elem1 &lt;- list1
     elem2 &lt;- list2 } yield List(elem1, elem2)
  </code></pre>
  
  <p>Or you can use the de-sugarized version:</p>
  
  <pre><code>list1.flatMap( elem1 =&gt; list2.map(elem2 =&gt; List(elem1,elem2)))
  </code></pre>
  
  <p>Following suite, I would like to create combinations of elements from N lists (N is known at runtime). Following the combinators example, 3 lists would be:</p>
  
  <pre><code>list1.flatMap( elem1 =&gt; list2.flatMap(elem2 =&gt; list3.map(elem3 =&gt; List(elem1,elem2,elem3)))
  </code></pre>
  
  <p>so I see the pattern and I know there's a recursion there, but I've been struggling to pin it down.</p>
  
  <pre><code>def combinations[T](lists:List[List[T]]): List[List[T]] = ???
  </code></pre>
  
  <p>Any ideas?</p>