Permutations: Difference between revisions

Added sections. Changed indentation of the Go translation. Replaced procedure variable with a simple procedure. Used "rotateLeft" from module "algorithm".
(Added sections. Changed indentation of the Go translation. Replaced procedure variable with a simple procedure. Used "rotateLeft" from module "algorithm".)
Line 5,831:
=={{header|Nim}}==
 
===Using the standard library===
<lang nim>import algorithm
var v = [1, 2, 3] # List has to start sorted
Line 5,848:
</pre>
 
===Translation of C===
{{trans|C}}
<lang nim># iterative Boothroyd method
Line 5,885 ⟶ 5,886:
@[3, 2, 1]</pre>
 
===Translation of Go===
{{trans|Go}}
<lang nim># nimNim implementation of the (very fast) Go example .
# http://rosettacode.org/wiki/Permutations#Go
# implementing a recursive https://en.wikipedia.org/wiki/Steinhaus–Johnson–Trotter_algorithm
 
import algorithm
proc perm( s: openArray[int], emit: proc(emit:openArray[int]) ) =
var s = @s
if s.len == 0:
emit(s)
return
 
proc perm(s: openArray[int]; var rc emit: proc(npemit: openArray[int])) =
var s = @s
rc = proc(np: int) =
if s.len == 0:
emit(s)
return
 
proc rc = proc(np: int) =
if np == 1:
if np == emit(s)1:
returnemit(s)
return
var
np1 = np - 1
pp = s.len - np1
rc(np1) # recurs prior swaps
 
rc(np1) # recursRecurse prior swaps.
for i in countDown(pp, 1):
swap s[i], s[i-1]
rc(np1) # recurs swap
let w = s[0]
s[0..<pp] = s[1..pp]
s[pp] = w
 
for i in countDown(pp, 1):
rc(s.len)
swap s[i], s[i-1]
rc(np1) # recursRecurse swap .
 
s.rotateLeft(0..pp, 1)
 
rc(s.len)
 
var se = @[0, 1, 2, 3] #, 4, 5, 6, 7, 8, 9, 10]
 
perm(se, proc(seqs: openArray[int])= echo s)</lang>
echo seq
)</lang>
 
=={{header|OCaml}}==
Anonymous user