First class environments: Difference between revisions

No edit summary
Line 1,008:
 
# Until all values are 1:
whileuntil envs.findall? {|e| e.instance_eval {@n} >== 1}
envs.each do |e|
e.instance_eval do # Use environment _e_
printf "%4s", @n
unless 1 ==if @n > 1
@cnt += 1 # Increment step count
@n = if 1 & @n.odd? == 1 # Calculate next hailstone value
@n * 3 + 1
else
Line 1,039:
binding
end
eval(<<-'eos'EOS, e).call(n)
n, cnt = nil, 0
proc {|arg| n = arg}
eosEOS
next e
end
 
# Until all values are 1:
whileuntil envs.findall? {|e| eval('n >== 1', e)}
envs.each do |e|
eval(<<-'eos'EOS, e) # Use environment _e_
printf "%4s", n
unlessif 1n ==> n1
cnt += 1 # Increment step count
n = if 1 & n.odd? == 1 # Calculate next hailstone value
n * 3 + 1
else
Line 1,059:
end
end
eosEOS
end
puts
Line 1,065:
puts '=' * 48
envs.each do |e| # For each environment _e_
eval(<<-'eosprintf "%4s", cnt', e) # print the step count
printf "%4s", cnt # print the step count
eos
end
puts</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
================================================
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
 
=={{header|Tcl}}==
Anonymous user