Skip to content

Instantly share code, notes, and snippets.

@zjor
Created March 22, 2013 04:12
Show Gist options
  • Save zjor/5218886 to your computer and use it in GitHub Desktop.
Save zjor/5218886 to your computer and use it in GitHub Desktop.
Generates permutations recursively
object Main {
def perms(a: List[Int]): List[List[Int]] = {
if (a.size <= 1) List(a)
else {
for {
p <- perms(a.tail)
i <- (0 to p.size).toList
} yield p.take(i) ::: List(a.head) ::: p.drop(i)
}
}
def main(args: Array[String]) {
print(perms(List(1, 2, 3)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment