Permutations: Difference between revisions

(added langur language example)
Line 6,617:
 
=={{header|Tailspin}}==
This solution seems to be the same as the Kotlin solution.
<lang tailspin>
templates permutations
Line 6,652 ⟶ 6,653:
def alpha: ['ABCD'...];
[ $alpha::length -> lexicalPermutations -> '$alpha($)...;' ] -> !OUT::write
</lang>
{{out}}
<pre>
[ABCD, ABDC, ACBD, ACDB, ADBC, ADCB, BACD, BADC, BCAD, BCDA, BDAC, BDCA, CABD, CADB, CBAD, CBDA, CDAB, CDBA, DABC, DACB, DBAC, DBCA, DCAB, DCBA]
</pre>
 
The solutions above create a lot of new arrays at various stages. We can also use mutable state and just emit a copy for each generated solution.
<lang tailspin>
templates perms
templates findPerms
<$@perms::length..> $@perms !
<>
def index: $;
$index..$@perms::length
-> (
@perms([$, $index]): $@perms([$index, $])...;
$index + 1 -> findPerms !
) !
@perms([$@perms::length, $index..~$@perms::length]): $@perms($index..-1)...;
end findPerms
@: [1..$];
1 -> findPerms !
end perms
def alpha: ['ABCD'...];
[4 -> perms -> '$alpha($)...;' ] -> !OUT::write
</lang>
{{out}}
Anonymous user