RosettaCodeData/Task/Permutations-by-swapping/Perl-6/permutations-by-swapping.pl6

13 lines
300 B
Raku
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
sub insert($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. +@xs) }
2013-04-10 23:57:08 -07:00
sub order($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
multi perms([]) {
[] => +1
}
multi perms([$x, *@xs]) {
2015-11-18 06:14:39 +00:00
perms(@xs).map({ |order($_.value, insert($x, $_.key)) }) Z=> |(+1,-1) xx *
2013-04-10 23:57:08 -07:00
}
.say for perms([0..2]);