Skip to content

Instantly share code, notes, and snippets.

@fwrq41251
Last active November 9, 2016 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwrq41251/d30e8db2183cd106fdb0c60b4ceeb32f to your computer and use it in GitHub Desktop.
Save fwrq41251/d30e8db2183cd106fdb0c60b4ceeb32f to your computer and use it in GitHub Desktop.
SICP
object Permutations {
def permutations[A](s: Set[A]): List[List[A]] = {
if (s.size == 1) List(s.toList)
else s.flatMap(x => permutations(s - x).map(p => List(x) ::: p)).toList
}
def main(args: Array[String]): Unit = {
println(permutations(Set(1, 2, 3)).mkString("\n"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment