title: How to make this recursive method tail recursive in Scala?
body: <p>I <a href="http://anders.janmyr.com/2009/10/lists-in-scala.html" rel="nofollow">found a function</a> for creating a cartesian product from a list of lists in Scala. However, it isn't tail recursive and won't work well with large lists. Unfortunately I won't know at design time how many lists I will need to combine, so I believe a recursive function is necessary. I'm struggling to make it tail recursive so it can be optimized by the compiler:</p>
  
  <pre><code>def product[T](listOfLists: List[List[T]]): List[List[T]] = listOfLists match {
      case Nil =&gt; List(List())
      case xs :: xss =&gt; for (y &lt;- xs; ys &lt;- product(xss)) yield y :: ys
  }
  </code></pre>