Permutations: Difference between revisions

→‎{{header|Perl 6}}: adding a simpler, recursive version
(→‎{{header|Erlang}}: Made the zipper implementation tail recursive)
(→‎{{header|Perl 6}}: adding a simpler, recursive version)
Line 1,800:
=={{header|Perl 6}}==
{{works with|rakudo|2012-10-24}}
Here is a quick and simple recursive implementation:
This is generic code that works with any ordered type. To force lexicographic ordering, change <tt>after</tt> to <tt>gt</tt>. To force numeric order, replace it with <tt>&gt;</tt>.
<lang Perl6>
multi postfix:<!>(@a where 1) { [@a] }
multi postfix:<!>(@a) {
gather for @a -> $a {
take [ $a, @$_ ] for @a.grep(none $a)!
}
}
 
say .perl for <a b c>!
</lang>
{{out}}
<pre>["a", "b", "c"]
["a", "c", "b"]
["b", "a", "c"]
["b", "c", "a"]
["c", "a", "b"]
["c", "b", "a"]</pre>
 
ThisHere is a generic code that works with any ordered type. To force lexicographic ordering, change <tt>after</tt> to <tt>gt</tt>. To force numeric order, replace it with <tt>&gt;</tt>.
<lang perl6>sub next_perm ( @a is copy ) {
my $j = @a.end - 1;
1,934

edits