First class environments: Difference between revisions

m (corrrected two misspellings, added a ;Task: section header, added whitespace before the TOC.)
Line 1,171:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|Nim}}==
{{trans|C}}
<lang Nim>import strformat
 
const Jobs = 12
 
type Environment = object
sequence: int
count: int
 
var
env: array[Jobs, Environment]
sequence, count: ptr int
 
#---------------------------------------------------------------------------------------------------
 
proc hail() =
stdout.write fmt"{sequence[]: 4d}"
if sequence[] == 1: return
inc count[]
sequence[] = if (sequence[] and 1) != 0: 3 * sequence[] + 1
else: sequence[] div 2
 
#---------------------------------------------------------------------------------------------------
 
proc switchTo(id: int) =
sequence = addr(env[id].sequence)
count = addr(env[id].count)
 
#---------------------------------------------------------------------------------------------------
 
template forAllJobs(statements: untyped): untyped =
for i in 0..<Jobs:
switchTo(i)
statements
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
for i in 0..<Jobs:
switchTo(i)
env[i].sequence = i + 1
 
var terminated = false
while not terminated:
 
forAllJobs:
hail()
echo ""
 
terminated = true
forAllJobs:
if sequence[] != 1:
terminated = false
break
 
echo ""
echo "Counts:"
forAllJobs:
stdout.write fmt"{count[]: 4d}"
echo ""</lang>
 
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
1 1 10 2 16 3 22 4 28 5 34 6
1 1 5 1 8 10 11 2 14 16 17 3
1 1 16 1 4 5 34 1 7 8 52 10
1 1 8 1 2 16 17 1 22 4 26 5
1 1 4 1 1 8 52 1 11 2 13 16
1 1 2 1 1 4 26 1 34 1 40 8
1 1 1 1 1 2 13 1 17 1 20 4
1 1 1 1 1 1 40 1 52 1 10 2
1 1 1 1 1 1 20 1 26 1 5 1
1 1 1 1 1 1 10 1 13 1 16 1
1 1 1 1 1 1 5 1 40 1 8 1
1 1 1 1 1 1 16 1 20 1 4 1
1 1 1 1 1 1 8 1 10 1 2 1
1 1 1 1 1 1 4 1 5 1 1 1
1 1 1 1 1 1 2 1 16 1 1 1
1 1 1 1 1 1 1 1 8 1 1 1
1 1 1 1 1 1 1 1 4 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1
 
Counts:
0 1 7 2 5 8 16 3 19 6 14 9</pre>
 
=={{header|Order}}==
Anonymous user