Permutations by swapping: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→Alternative Iterative version: Fix comment: Perl 6 --> Raku) |
(→{{header|F_Sharp|F#}}: Nicer interface) |
||
Line 914: | Line 914: | ||
let ni = [|for n in N -> 0|] |
let ni = [|for n in N -> 0|] |
||
let gel = Array.length(N)-1 |
let gel = Array.length(N)-1 |
||
yield |
yield N |
||
let rec _Ni g e l = seq{ |
let rec _Ni g e l = seq{ |
||
match (l,g) with |
match (l,g) with |
||
|_ when l<0 -> gn.[g] <- -gn.[g]; yield! _Ni (g-1) e (ni.[g-1] + gn.[g-1]) |
|_ when l<0 -> gn.[g] <- -gn.[g]; yield! _Ni (g-1) e (ni.[g-1] + gn.[g-1]) |
||
|(1,0) -> |
|(1,0) -> () |
||
|_ when l=g+1 -> gn.[g] <- -gn.[g]; yield! _Ni (g-1) (e+1) (ni.[g-1] + gn.[g-1]) |
|_ when l=g+1 -> gn.[g] <- -gn.[g]; yield! _Ni (g-1) (e+1) (ni.[g-1] + gn.[g-1]) |
||
|_ -> let n = N.[g-ni.[g]+e]; |
|_ -> let n = N.[g-ni.[g]+e]; |
||
N.[g-ni.[g]+e] <- N.[g-l+e]; N.[g-l+e] <- n; yield |
N.[g-ni.[g]+e] <- N.[g-l+e]; N.[g-l+e] <- n; yield N |
||
ni.[g] <- l; yield! _Ni gel 0 (ni.[gel] + gn.[gel])} |
ni.[g] <- l; yield! _Ni gel 0 (ni.[gel] + gn.[gel])} |
||
yield! _Ni gel 0 1 |
yield! _Ni gel 0 1 |
||
Line 932: | Line 932: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[|1; 2; 3; 4|] |
|||
[|1; 2; 4; 3|] |
|||
[|1; 4; 2; 3|] |
|||
[|4; 1; 2; 3|] |
|||
[|4; 1; 3; 2|] |
|||
[|1; 4; 3; 2|] |
|||
[|1; 3; 4; 2|] |
|||
[|1; 3; 2; 4|] |
|||
[|3; 1; 2; 4|] |
|||
[|3; 1; 4; 2|] |
|||
[|3; 4; 1; 2|] |
|||
[|4; 3; 1; 2|] |
|||
[|4; 3; 2; 1|] |
|||
[|3; 4; 2; 1|] |
|||
[|3; 2; 4; 1|] |
|||
[|3; 2; 1; 4|] |
|||
[|2; 3; 1; 4|] |
|||
[|2; 3; 4; 1|] |
|||
[|2; 4; 3; 1|] |
|||
[|4; 2; 3; 1|] |
|||
[|4; 2; 1; 3|] |
|||
[|2; 4; 1; 3|] |
|||
[|2; 1; 4; 3|] |
|||
[|2; 1; 3; 4|] |
|||
< |
v</pre> |
||
</pre> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |