Code to enumerate permutations in Scala

后端 未结 11 2059
臣服心动
臣服心动 2020-12-02 16:06

I coded a function to enumerate all permutations of a given list. What do you think of the code below?

def interleave(x:Int, l:List[Int]):List[List[Int]] = {         


        
11条回答
  •  渐次进展
    2020-12-02 16:25

    Here is a version based on span.

    def perms[T](xs: List[T]): List[List[T]] = xs match {
      case List(_) => List(xs)
      case _ => for ( x <- xs
                    ; val (l, r) = xs span { x!= }
                    ; ys <- perms(l ++ r.tail)
                    ) yield x :: ys
    }
    

提交回复
热议问题