Scala: List to List of Lists, idiomatically?
<p>In Scala, I wrote a Higher Order Function that iterates a list, and calls back with the head and list, for each element, that is:</p>

<pre><code>  def headAndSelf[A, B](li: List[A], f: (A, List[A]) =&gt; B): List[B] = { 
    if (li == Nil) Nil
    else f(li.head, li) :: headAndSelf(li.tail, f)
  }
</code></pre>

<p>Given <code>val a = List(1,2,3)</code> </p>

<pre><code>scala&gt; headAndSelf(a, (x: Int, y: List[Int]) =&gt; (x,y))
res4: List[(Int, List[Int])] = List((1,List(1, 2, 3)), (2,List(2, 3)), (3,List(3))) 
</code></pre>

<p>It then occurred to me I could get the head from the list, so it was simpler to write:</p>

<pre><code>  def self[A, B](li: List[A], f: (List[A]) =&gt; B): List[B] = { 
    if (li == Nil) Nil
    else f(li) :: self(li.tail, f)
  }
</code></pre>

<p>(even though it makes the lambda a bit less elegant)</p>

<pre><code>scala&gt; self(a, (x: List[Int]) =&gt; x)
res7: List[List[Int]] = List(List(1, 2, 3), List(2, 3), List(3))   
</code></pre>

<p>Then I thought, surely there's an even easier, idiomatic way to turn a List into a List of Lists. So what is it?</p>

<p>Bonus Question: why can't Scala infer the type of the lambda? <code>a</code> is a <code>List[Int]</code>, so <code>x</code> should be a <code>List[Int]</code> too, no?:</p>

<pre><code>scala&gt; self(a, x =&gt; x)
&lt;console&gt;:13: error: missing parameter type
       self(a, x =&gt; x)
               ^      
</code></pre>