title: Code to enumerate permutations in Scala
body: <p>I coded a function to enumerate all permutations of a given list. What do you think of the code below?</p>
  
  <pre><code>def interleave(x:Int, l:List[Int]):List[List[Int]] = {
    l match { 
      case Nil => List(List(x))
      case (head::tail) =>
        (x :: head :: tail) :: interleave(x, tail).map(head :: _)
    }
  }
  
  def permutations(l:List[Int]):List[List[Int]] = {
    l match {
      case Nil => List(List())
      case (head::tail) =>
        for(p0 &lt- permutations(tail); p1 &lt- interleave(head, p0)) yield p1
    }
  }
  </code></pre>