First class environments: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added Haskell solution) |
|||
Line 898: | Line 898: | ||
1 1 1 1 1 1 1 1 2 1 1 1 |
1 1 1 1 1 1 1 1 2 1 1 1 |
||
Counts |
Counts |
||
0 1 7 2 5 8 16 3 19 6 14 9 |
|||
</pre> |
|||
=={{header|Phix}}== |
|||
Emulation using edx as an "enviroment index" into static sequences: |
|||
<lang Phix>function hail(integer n) |
|||
if remainder(n,2)=0 then |
|||
n /= 2 |
|||
else |
|||
n = 3*n+1 |
|||
end if |
|||
return n |
|||
end function |
|||
sequence hails = tagset(12), |
|||
counts = repeat(0,12), |
|||
results = columnize({hails}) |
|||
function step(integer edx) |
|||
integer n = hails[edx] |
|||
if n=1 then return 0 end if |
|||
n = hail(n) |
|||
hails[edx] = n |
|||
counts[edx] += 1 |
|||
results[edx] &= n |
|||
return 1 |
|||
end function |
|||
procedure main() |
|||
bool done = false |
|||
while not done do |
|||
done = true |
|||
for i=1 to 12 do |
|||
if step(i) then |
|||
done = false |
|||
end if |
|||
end for |
|||
end while |
|||
for i=1 to max(counts)+1 do |
|||
for j=1 to 12 do |
|||
puts(1,iff(i<=length(results[j])?sprintf("%4d",{results[j][i]}):" ")) |
|||
end for |
|||
puts(1,"\n") |
|||
end for |
|||
printf(1," %s\n",{join(repeat("===",12))}) |
|||
for j=1 to 12 do |
|||
printf(1,"%4d",{counts[j]}) |
|||
end for |
|||
puts(1,"\n") |
|||
end procedure |
|||
main()</lang> |
|||
Emulation using dictionaries: |
|||
<lang Phix> |
|||
function hail(integer n) |
|||
if remainder(n,2)=0 then |
|||
n /= 2 |
|||
else |
|||
n = 3*n+1 |
|||
end if |
|||
return n |
|||
end function |
|||
function step(integer edx) |
|||
integer n = getd("hail",edx) |
|||
if n=1 then return 0 end if |
|||
n = hail(n) |
|||
setd("hail",n,edx) |
|||
setd("count",getd("count",edx)+1,edx) |
|||
setd("results",getd("results",edx)&n,edx) |
|||
return 1 |
|||
end function |
|||
sequence dicts = {} |
|||
procedure main() |
|||
for i=1 to 12 do |
|||
integer d = new_dict() |
|||
setd("hail",i,d) |
|||
setd("count",0,d) |
|||
setd("results",{i},d) |
|||
dicts &= d |
|||
end for |
|||
bool done = false |
|||
while not done do |
|||
done = true |
|||
for i=1 to 12 do |
|||
if step(dicts[i]) then |
|||
done = false |
|||
end if |
|||
end for |
|||
end while |
|||
done = false |
|||
integer i = 1 |
|||
while not done do |
|||
done = true |
|||
for j=1 to 12 do |
|||
sequence res = getd("results",dicts[j]) |
|||
if i<length(res) then done = false end if |
|||
puts(1,iff(i<=length(res)?sprintf("%4d",{res[i]}):" ")) |
|||
end for |
|||
puts(1,"\n") |
|||
i += 1 |
|||
end while |
|||
printf(1," %s\n",{join(repeat("===",12))}) |
|||
for j=1 to 12 do |
|||
integer count = getd("count",dicts[j]) |
|||
printf(1,"%4d",{count}) |
|||
end for |
|||
puts(1,"\n") |
|||
end procedure |
|||
main()</lang> |
|||
{{out}} |
|||
(same for both) |
|||
<pre> |
|||
1 2 3 4 5 6 7 8 9 10 11 12 |
|||
1 10 2 16 3 22 4 28 5 34 6 |
|||
5 1 8 10 11 2 14 16 17 3 |
|||
16 4 5 34 1 7 8 52 10 |
|||
8 2 16 17 22 4 26 5 |
|||
4 1 8 52 11 2 13 16 |
|||
2 4 26 34 1 40 8 |
|||
1 2 13 17 20 4 |
|||
1 40 52 10 2 |
|||
20 26 5 1 |
|||
10 13 16 |
|||
5 40 8 |
|||
16 20 4 |
|||
8 10 2 |
|||
4 5 1 |
|||
2 16 |
|||
1 8 |
|||
4 |
|||
2 |
|||
1 |
|||
=== === === === === === === === === === === === |
|||
0 1 7 2 5 8 16 3 19 6 14 9 |
0 1 7 2 5 8 16 3 19 6 14 9 |
||
</pre> |
</pre> |