First class environments: Difference between revisions

J
(Go solution)
(J)
Line 211:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|J}}==
 
I have tried to determine what makes this task interesting (see talk page), but I am still confused.
 
Here is my current interpretation of the task requirements:
 
<lang j>coclass 'hailstone'
 
step=:3 :0
NB. and determine next element in hailstone sequence
if.1=N do. N return.end.
NB. count how many times this has run when N was not 1
STEP=:STEP+1
if.0=2|N do.
N=: N%2
else.
N=: 1 + 3*N
end.
)
 
create=:3 :0
STEP=: 0
N=: y
)
 
current=:3 :0
N__y
)
 
run1=:3 :0
step__y''
STEP__y
)
 
run=:3 :0
old=: ''
while. -. old -: state=: run1"0 y do.
smoutput 4j0 ": current"0 y
old=: state
end.
)</lang>
 
Example use:
 
<lang j> environments=: conew&'hailstone'"0 (1+i.12)
run_hailstone_ environments
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
1 1 1 1 1 1 1 1 1 1 1 1
0 1 7 2 5 8 16 3 19 6 14 9</lang>
 
In essence: run is a static method of the class <code>hailstone</code> which, given a list of objects of the class runs all of them until their hailstone sequence number stops changing. It also displays the hailstone sequence number from each of the objects at each step. Its result is the step count from each object.
 
=={{header|PicoLisp}}==
6,962

edits