Sattolo cycle: Difference between revisions
Content added Content deleted
Line 2,204: | Line 2,204: | ||
[10, 20, 30] shuffled = [20, 30, 10] |
[10, 20, 30] shuffled = [20, 30, 10] |
||
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22] shuffled = [20, 22, 17, 12, 19, 14, 15, 13, 21, 16, 11, 18]</pre> |
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22] shuffled = [20, 22, 17, 12, 19, 14, 15, 13, 21, 16, 11, 18]</pre> |
||
=={{header|Transd}}== |
|||
<syntaxhighlight lang="Scheme">#lang transd |
|||
MainModule: { |
|||
// Define an abstract type Vec to make the shuffling |
|||
// function polymorphic |
|||
Vec: typedef(Lambda<:Data Bool>(λ d :Data() |
|||
(starts-with (_META_type d) "Vector<"))), |
|||
sshuffle: (λ v Vec() locals: rnd 0 |
|||
(for n in Range( (- (size v) 1) 0) do |
|||
(= rnd (randr (to-Int (- n 1)))) |
|||
(with tmp (cp (get v n)) |
|||
(set-el v n (get v rnd)) |
|||
(set-el v rnd tmp)) |
|||
) |
|||
(lout v) |
|||
), |
|||
_start: (λ |
|||
(with v [10,20,30,40,50,60,70,80,90,100] |
|||
(lout "Original:\n" v) |
|||
(lout "Shuffled:") |
|||
(sshuffle v)) |
|||
(lout "") |
|||
(with v ["A","B","C","D","E","F","G","H"] |
|||
(lout "Original:\n" v) |
|||
(lout "Shuffled:") |
|||
(sshuffle (cp v))) |
|||
) |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Original: |
|||
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100] |
|||
Shuffled: |
|||
[20, 90, 100, 50, 30, 10, 60, 70, 40, 80] |
|||
Original: |
|||
["A", "B", "C", "D", "E", "F", "G", "H"] |
|||
Shuffled: |
|||
["E", "A", "H", "B", "G", "D", "C", "F"] |
|||
</pre> |
|||
=={{header|TypeScript}}== |
=={{header|TypeScript}}== |