Skip to content

Instantly share code, notes, and snippets.

Created October 10, 2012 20:39
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 anonymous/3868275 to your computer and use it in GitHub Desktop.
Save anonymous/3868275 to your computer and use it in GitHub Desktop.
permutations (recursive)
sub insert($x, @xs) { [@xs[0..$_-1], $x, @xs[$_..*]] for 0..+@xs }
sub order($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
multi perms([]) {
[] => +1
}
multi perms([$x, *@xs]) {
perms(@xs).map({ order($_.value, insert($x, $_.key)) }) Z=> (+1,-1) xx *
}
.say for perms([0..2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment