First class environments: Difference between revisions

Added Kotlin
m (→‎{{header|REXX}}: changed a comment in the REXX section header.)
(Added Kotlin)
Line 739:
Counts:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|Kotlin}}==
{{trans|C}}
<lang scala>// version 1.1.3
 
class Environment(var seq: Int, var count: Int)
 
const val JOBS = 12
val envs = List(JOBS) { Environment(it + 1, 0) }
var env = envs[0] // stores reference to 'current' environment
 
fun hailstone() {
print("%4d".format(env.seq))
if (env.seq == 1) return
env.count++
env.seq = if (env.seq % 2 == 1) 3 * env.seq + 1 else env.seq / 2
}
 
fun switchTo(id: Int) {
env = envs[id]
}
 
fun code() {
do {
for (a in 0 until JOBS) {
switchTo(a)
hailstone()
}
println()
}
while (envs.any { it.seq != 1 } )
 
println("\nCOUNTS:")
for (a in 0 until JOBS) {
switchTo(a)
print("%4d".format(env.count))
}
println()
}
fun main(args: Array<String>) {
code()
}</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>
 
9,482

edits