Last active
November 9, 2016 01:51
-
-
Save fwrq41251/d30e8db2183cd106fdb0c60b4ceeb32f to your computer and use it in GitHub Desktop.
SICP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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