Created
October 10, 2012 20:39
-
-
Save anonymous/3868275 to your computer and use it in GitHub Desktop.
permutations (recursive)
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
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