11 lines
255 B
Scala
11 lines
255 B
Scala
def permutations[T]: List[T] => Traversable[List[T]] = {
|
|
case Nil => List(Nil)
|
|
case xs => {
|
|
for {
|
|
(x, i) <- xs.zipWithIndex
|
|
ys <- permutations(xs.take(i) ++ xs.drop(1 + i))
|
|
} yield {
|
|
x :: ys
|
|
}
|
|
}
|
|
}
|