Permutations: Difference between revisions

(Liberty BASIC added)
(→‎{{header|Perl 6}}: use built-in)
Line 2,269:
 
=={{header|Perl 6}}==
{{works with|rakudo|20122014-101-24}}
First, you can just use the built-in method on any list type.
Here is a quick and simple recursive implementation:
<lang Perl6>sub.say for postfix:<!>(@a) {b c>.permutations</lang>
@a == 1 ?? [@a] !!
gather for @a -> $a {
take [ $a, @$_ ] for @a.grep(none $a)!
}
}
 
.say for <a b c>!</lang>
{{out}}
<pre>a b c
Line 2,287 ⟶ 2,280:
c b a</pre>
 
Here is asome 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;
Line 2,312 ⟶ 2,305:
c b a
</pre>
Here is aanother non-recursive implementation, which returns a lazy list. It also works with any type.
<lang perl6>sub permute(@items) {
sub permute(@items) {
my @seq := 1..+@items;
gather for (^[*] @seq) -> $n is copy {
Line 2,326 ⟶ 2,318:
}
}
.say for permute( 'a'..'c' )</lang>
</lang>
{{out}}
<pre>a b c
Line 2,336 ⟶ 2,327:
c b a
</pre>
Finally, if you just want zero-based numbers, you can call the built-in function:
<lang perl6>.say for permutations(3);</lang>
{{out}}
<pre>0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0</pre>
 
=={{header|PicoLisp}}==
Anonymous user