First class environments: Difference between revisions
Added FreeBASIC
(Omit this from Lily: It does not support eval or anything like that.) |
(Added FreeBASIC) |
||
(38 intermediate revisions by 14 users not shown) | |||
Line 4:
Often this term is used in the context of "first class functions". In an analogous way, a programming language may support "first class environments".
The environment is minimally, the set of variables
Often an environment is captured in a [[wp:Closure_(computer_science)|closure]], which encapsulates a function together with an environment. That environment, however, is '''not''' first-class, as it cannot be created, passed etc. independently from the function's code.
Line 10:
Therefore, a first class environment is a set of variable bindings which can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable. It is like a closure without code. A statement must be able to be executed within a stored first class environment and act according to the environment variable values stored within.
;Task:
Build a dozen environments, and a single piece of code to be run repeatedly in each of these environments.
Each environment contains the bindings for two variables:
:* a value in the [[Hailstone sequence]], and
:* a count which is incremented until the value drops to 1.
The initial hailstone values are 1 through 12, and the count in each environment is zero.
When the code runs, it calculates the next hailstone step in the current environment (unless the value is already 1) and counts the steps. Then it prints the current value in a tabular form.
When all hailstone values dropped to 1, processing stops, and the total number of hailstone steps for each environment is printed.
<br><br>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Here the 'environment' consists of all the dynamic variables; the static integer variables (A%-Z%) are not affected.
<
@% = 4 : REM Column width
Line 65 ⟶ 74:
IF LEN(e$) < 216 e$ = STRING$(216, CHR$0)
SYS "RtlMoveMemory", ^@%+108, !^e$, 216
ENDPROC</
'''Output:'''
<pre>
Line 93 ⟶ 102:
=={{header|Bracmat}}==
<
& :?environments
& 13:?seq
Line 126 ⟶ 135:
)
& out$(After !environments)
)</
Output:
<pre> Before
Line 176 ⟶ 185:
=={{header|C}}==
Well, this fits the semantics, not sure about the spirit…
<
#define JOBS 12
Line 211 ⟶ 220:
return 0;
}</
{{out}}
<pre>
Line 236 ⟶ 245:
COUNTS:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
(def hailstone-src
"(defn hailstone-step [env]
(let [{:keys[n cnt]} env]
(cond
(= n 1) {:n 1 :cnt cnt}
(even? n) {:n (/ n 2) :cnt (inc cnt)}
:else {:n (inc (* n 3)) :cnt (inc cnt)})))")
(defn create-hailstone-table [f-src]
(let [done? (fn [e] (= (:n e) 1))
print-step (fn [envs] (println (map #(format "%4d" (:n %)) envs)))
print-counts (fn [envs] (println "Counts:\n"
(map #(format "%4d" (:cnt %)) envs)))]
(loop [f (eval (read-string f-src))
envs (for [n (range 12)]
{:n (inc n) :cnt 0})]
(if (every? done? envs)
(print-counts envs)
(do
(print-step envs)
(recur f (map f envs)))))))
</syntaxhighlight>
{{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>
Line 241 ⟶ 301:
D doesn't have first class environments, this is an approximation.
{{trans|Python}}
<
struct Prop {
Line 268 ⟶ 328:
writefln("Counts:\n%(% 4d%)", envs.map!(env => env.cnt));
}</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
Line 291 ⟶ 351:
Counts:
0 1 7 2 5 8 16 3 19 6 14 9</pre>
=={{header|EchoLisp}}==
'''(environment-new ((name value) ..) ''' is used to create a new envrionment. '''(eval form env)''' is used to evaluate a form in a specified environment.
<syntaxhighlight lang="scheme">
(define (bump-value)
(when (> value 1)
(set! count (1+ count))
(set! value (if (even? value) (/ value 2) (1+ (* 3 value))))))
(define (env-show name envs )
(write name)
(for ((env envs)) (write (format "%4a" (eval name env))))
(writeln))
(define (task (envnum 12))
(define envs (for/list ((i envnum)) (environment-new `((value ,(1+ i)) (count 0)))))
(env-show 'value envs)
(while
(any (curry (lambda ( n env) (!= 1 (eval n env))) 'value) envs)
(for/list ((env envs)) (eval '(bump-value) env))
(env-show 'value envs))
(env-show 'count envs))
</syntaxhighlight>
{{out}}
<pre>
(task)
value 1 2 3 4 5 6 7 8 9 10 11 12
value 1 1 10 2 16 3 22 4 28 5 34 6
value 1 1 5 1 8 10 11 2 14 16 17 3
value 1 1 16 1 4 5 34 1 7 8 52 10
value 1 1 8 1 2 16 17 1 22 4 26 5
value 1 1 4 1 1 8 52 1 11 2 13 16
value 1 1 2 1 1 4 26 1 34 1 40 8
value 1 1 1 1 1 2 13 1 17 1 20 4
value 1 1 1 1 1 1 40 1 52 1 10 2
value 1 1 1 1 1 1 20 1 26 1 5 1
value 1 1 1 1 1 1 10 1 13 1 16 1
value 1 1 1 1 1 1 5 1 40 1 8 1
value 1 1 1 1 1 1 16 1 20 1 4 1
value 1 1 1 1 1 1 8 1 10 1 2 1
value 1 1 1 1 1 1 4 1 5 1 1 1
value 1 1 1 1 1 1 2 1 16 1 1 1
value 1 1 1 1 1 1 1 1 8 1 1 1
value 1 1 1 1 1 1 1 1 4 1 1 1
value 1 1 1 1 1 1 1 1 2 1 1 1
value 1 1 1 1 1 1 1 1 1 1 1 1
count 0 1 7 2 5 8 16 3 19 6 14 9
</pre>
=={{header|Erlang}}==
Line 301 ⟶ 409:
</pre>
There is a lot of code below to manage the tabular printout. Otherwise the task is simple.
<syntaxhighlight lang="erlang">
-module( first_class_environments ).
Line 361 ⟶ 469:
end,
print_loop().
</syntaxhighlight>
{{out}}
<pre>
Line 381 ⟶ 489:
[{12,9}, {11,14}, {10,6}, {9,19}, {8,3}, {7,16}, {6,8}, {5,5}, {4,2}, {3,7}, {2,1}, {1,0}]
</pre>
=={{header|Factor}}==
Factor is a stack language without the need for variable bindings. Values are variables through and through. This simplifies matters somewhat. It means we can use data stacks (sequences) for our first class environments. The <code>with-datastack</code> combinator takes a data stack (sequence) and quotation as input, and inside the quotation, it is as though one is operating on a new data stack populated with values from the sequence. The resultant data stack is then once again stored as a sequence for safe keeping.
<syntaxhighlight lang="factor">USING: assocs continuations formatting io kernel math
math.ranges sequences ;
: (next-hailstone) ( count value -- count' value' )
[ 1 + ] [ dup even? [ 2/ ] [ 3 * 1 + ] if ] bi* ;
: next-hailstone ( count value -- count' value' )
dup 1 = [ (next-hailstone) ] unless ;
: make-environments ( -- seq ) 12 [ 0 ] replicate 12 [1,b] zip ;
: step ( seq -- new-seq )
[ [ dup "%4d " printf next-hailstone ] with-datastack ] map
nl ;
: done? ( seq -- ? ) [ second 1 = ] all? ;
make-environments
[ dup done? ] [ step ] until nl
"Counts:" print
[ [ drop "%4d " printf ] with-datastack drop ] each nl</syntaxhighlight>
{{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|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Type E
As Integer _valor, _contar
Public:
Declare Sub Constructor_(value As Integer, count As Integer)
Declare Function Valor() As Integer
Declare Function Contar() As Integer
Declare Sub Hailstone()
End Type
Sub E.Constructor_(value As Integer, count As Integer)
This._valor = value
This._contar = count
End Sub
Function E.Valor() As Integer
Return This._valor
End Function
Function E.Contar() As Integer
Return This._contar
End Function
Sub E.Hailstone()
Print Using "####"; This._valor;
If (This._valor = 1) Then Exit Sub
This._contar = This._contar + 1
This._valor = Iif(This._valor Mod 2 = 0, This._valor \ 2, 3 * This._valor + 1)
End Sub
Dim As Integer jobs = 12
Dim As E envs(jobs)
For i As Integer = 0 To jobs - 1
envs(i).Constructor_(i + 1, 0)
Next i
Print "Sequences:"
Dim As Integer done = 0
While done = 0
For i As Integer = 0 To jobs - 1
envs(i).Hailstone()
Next i
Print
done = 1
For i As Integer = 0 To jobs - 1
If envs(i).Valor() <> 1 Then
done = 0
Exit For
End If
Next i
Wend
Print "Counts:"
For i As Integer = 0 To jobs - 1
Print Using "####"; envs(i).Contar();
Next i
Print
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Wren entry.</pre>
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">package main
import "fmt"
const jobs = 12
type environment struct{ seq, cnt int }
var (
env [jobs]environment
seq, cnt *int
)
func hail() {
fmt.Printf("% 4d", *seq)
if *seq == 1 {
return
}
(*cnt)++
if *seq&1 != 0 {
*seq = 3*(*seq) + 1
} else {
*seq /= 2
}
}
func switchTo(id int) {
seq = &env[id].seq
cnt = &env[id].cnt
}
func main() {
for i := 0; i < jobs; i++ {
switchTo(i)
env[i].seq = i + 1
}
again:
for i := 0; i < jobs; i++ {
switchTo(i)
hail()
}
fmt.Println()
for j := 0; j < jobs; j++ {
switchTo(j)
if *seq != 1 {
goto again
}
}
fmt.Println()
fmt.Println("COUNTS:")
for i := 0; i < jobs; i++ {
switchTo(i)
fmt.Printf("% 4d", *cnt)
}
fmt.Println()
}</syntaxhighlight>
{{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|Haskell}}==
First let's implement the algorithm of calculating Hailstone series:
<syntaxhighlight lang="haskell">hailstone n
| n == 1 = 1
| even n = n `div` 2
| odd n = 3*n + 1</syntaxhighlight>
and a data structure representing the environment
<syntaxhighlight lang="haskell">data Environment = Environment { count :: Int, value :: Int }
deriving Eq</syntaxhighlight>
In Haskell operations with first class environments could be implemented using several approaches:
1. Using any data structure <code>S</code> which is passed by a chain of functions, having type <code>S -> S</code>.
2. Using the <code>Reader</code> monad, which emulates access to imutable environment.
3. Using the <code>State</code> monad, which emulates access to an environment, that coud be changed.
For given task approaches 1 and 3 are suitable.
Let's define a collection of environments:
<syntaxhighlight lang="haskell">environments = [ Environment 0 n | n <- [1..12] ]</syntaxhighlight>
and a <code>process</code>, which changes an environment according to a task.
Approach 1.
<syntaxhighlight lang="haskell">process (Environment c 1) = Environment c 1
process (Environment c n) = Environment (c+1) (hailstone n)</syntaxhighlight>
Approach 3. (needs <code>import Control.Monad.State</code>)
<syntaxhighlight lang="haskell">process = execState $ do
n <- gets value
c <- gets count
when (n > 1) $ modify $ \env -> env { count = c + 1 }
modify $ \env -> env { value = hailstone n }</syntaxhighlight>
Repetitive batch processing of a collection we implement as following:
<syntaxhighlight lang="haskell">fixedPoint f x
| fx == x = [x]
| otherwise = x : fixedPoint f fx
where fx = f x
prettyPrint field = putStrLn . foldMap (format.field)
where format n = (if n < 10 then " " else "") ++ show n ++ " "
main = do
let result = fixedPoint (map process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
prettyPrint count (last result)</syntaxhighlight>
{{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
1 1 1 1 1 1 1 1 1 1 1 1
-----------------------------------
0 1 7 2 5 8 16 3 19 6 14 9 </pre>
Or in "transposed" way
<syntaxhighlight lang="haskell">main = do
let result = map (fixedPoint process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
putStrLn "Counts: "
prettyPrint (count . last) result</syntaxhighlight>
{{Out}}
<pre> 1
2 1
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
6 3 10 5 16 8 4 2 1
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 4 2 1
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 5 16 8 4 2 1
11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
12 6 3 10 5 16 8 4 2 1
------------------------------------
Counts:
0 1 7 2 5 8 16 3 19 6 14 9 </pre>
=={{header|Icon}} and {{header|Unicon}}==
The simplest way to create an environment with variables isolated from code in Icon/Unicon is to create instances of records or class objects.
<
procedure main()
Line 407 ⟶ 826:
else env.sequence := 3 * env.sequence + 1
}
end</
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
Line 439 ⟶ 858:
Here is my current interpretation of the task requirements:
<
step=:3 :0
Line 473 ⟶ 892:
old=: state
end.
)</
{{out|Example use}}
<
run_hailstone_ environments
1 1 10 2 16 3 22 4 28 5 34 6
Line 496 ⟶ 915:
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</
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|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class FirstClassEnvironments {
public static void main(String[] aArgs) {
code();
}
private static void code() {
do {
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
hailstone();
}
System.out.println();
} while ( ! allDone() );
System.out.println(System.lineSeparator() + "Counts:");
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
System.out.print(String.format("%4d", count));
}
System.out.println();
}
private static boolean allDone() {
for ( int job = 0; job < JOBS; job++ ) {
switchTo(job);
if ( sequence > 1 ) {
return false;
}
}
return true;
}
private static void hailstone() {
System.out.print(String.format("%4d", sequence));
if ( sequence == 1 ) {
return;
}
count += 1;
sequence = ( sequence % 2 == 1 ) ? 3 * sequence + 1 : sequence / 2;
}
private static void switchTo(int aID) {
if ( aID != currentId ) {
environments.get(currentId).seq = sequence;
environments.get(currentId).count = count;
currentId = aID;
}
sequence = environments.get(aID).seq;
count = environments.get(aID).count;
}
private static class Environment {
public Environment(int aSeq, int aCount) {
seq = aSeq; count = aCount;
}
private int seq, count;
}
private static int sequence, count, currentId;
private static List<Environment> environments =
IntStream.rangeClosed(1, 12).mapToObj( i -> new Environment(i, 0 ) ).collect(Collectors.toList());
private static final int JOBS = 12;
}
</syntaxhighlight>
{{ 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|jq}}==
{{works with|jq|1.5}}
In the context of jq, a JSON object is an environment in the sense of this article,
because an object, E, serves as an execution environment for a program of the form E | P
where P is a jq program.
For the task at hand, the environment can be taken to be an object of the form:
{ "value": <HAILSTONE>, "count": <COUNT> }
The required jq "code" is simply:
if .value > 1 then (.value |= hail) | .count += 1 else . end
where the filter "hail", when given an integer, computes the next hailstone value.
Let us therefore define a function named "code" accordingly:
<syntaxhighlight lang="jq">def code:
# Given an integer as input, compute the corresponding hailstone value:
def hail: if . % 2 == 0 then ./2|floor else 3*. + 1 end;
if .value > 1 then (.value |= hail) | .count += 1 else . end;
</syntaxhighlight>
To generate the n-th environment, it is useful to define a function:
def environment: . as $n | { value: $n, count:0 };
Now the program for creating the 12 environments and applying "code" to them until quiesence can be written as follows:
def generate:
[range(1;13) | environment] # create 12 environments
| recurse( if (any(.[] | .value; . != 1)) then map(code) else empty end);
Finally, to present the results in tabular form, the following helper function will be useful:
# Apply a filter to a stream,
# and ALSO emit the last item in the stream filtered through "final"
def filter_and_last(s; filter; final):
[s] as $array
| ($array[] | filter), ($array[-1] | final);
Putting it all together:
<syntaxhighlight lang="jq">filter_and_last( generate;
map(.value) | @tsv;
"", "Counts:", (map(.count) | @tsv ))</syntaxhighlight>
{{out}}
The following invocation produces the result shown below, assuming the above code is in a file named "program.jq":
$ jq -nr -f program.jq
<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
1 1 1 1 1 1 1 1 1 1 1 1
Counts:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
=={{header|Julia}}==
{{trans|C}}
<syntaxhighlight lang="julia">const jobs = 12
mutable struct Environment
seq::Int
cnt::Int
Environment() = new(0, 0)
end
const env = [Environment() for i in 1:jobs]
const currentjob = [1]
seq() = env[currentjob[1]].seq
cnt() = env[currentjob[1]].cnt
seq(n) = (env[currentjob[1]].seq = n)
cnt(n) = (env[currentjob[1]].cnt = n)
function hail()
print(lpad(seq(), 4))
if seq() == 1
return
end
cnt(cnt() + 1)
seq(isodd(seq()) ? 3 * seq() + 1 : div(seq(), 2))
end
function runtest()
for i in 1:jobs
currentjob[1] = i
env[i].seq = i
end
computing = true
while computing
for i in 1:jobs
currentjob[1] = i
hail()
end
println()
for j in 1:jobs
currentjob[1] = j
if seq() != 1
break
elseif j == jobs
computing = false
end
end
end
println("\nCOUNTS:")
for i in 1:jobs
currentjob[1] = i
print(lpad(cnt(), 4))
end
println()
end
runtest()
</syntaxhighlight>{{output}}<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|Kotlin}}==
This is based on the C entry except that, instead of using object references (Kotlin/JVM doesn't support explicit pointers) to switch between environments, it saves and restores the actual values of the two variables on each job switch. I see no reason why objects shouldn't be used to represent the environments as long as they have no member functions.
<syntaxhighlight 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 seq = 0 // 'seq' for current environment
var count = 0 // 'count' for current environment
var currId = 0 // index of current environment
fun switchTo(id: Int) {
if (id != currId) {
envs[currId].seq = seq
envs[currId].count = count
currId = id
}
seq = envs[id].seq
count = envs[id].count
}
fun hailstone() {
print("%4d".format(seq))
if (seq == 1) return
count++
seq = if (seq % 2 == 1) 3 * seq + 1 else seq / 2
}
val allDone get(): Boolean {
for (a in 0 until JOBS) {
switchTo(a)
if (seq != 1) return false
}
return true
}
fun code() {
do {
for (a in 0 until JOBS) {
switchTo(a)
hailstone()
}
println()
}
while (!allDone)
println("\nCOUNTS:")
for (a in 0 until JOBS) {
switchTo(a)
print("%4d".format(count))
}
println()
}
fun main(args: Array<String>) {
code()
}</syntaxhighlight>
{{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|Lua}}==
Line 506 ⟶ 1,282:
* Lua 5.2: an upvalue called <code>_ENV</code>
<
local envs = { }
for i = 1, 12 do
Line 540 ⟶ 1,316:
io.write(("% 4d"):format(env.count))
end
</syntaxhighlight>
{{out}}
Line 566 ⟶ 1,342:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
=={{header|Nim}}==
{{trans|C}}
<syntaxhighlight 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 ""</syntaxhighlight>
{{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}}==
Order supports environments as a first-class type, but since all values are immutable, updating a value means using one environment to update the next in a chain (nothing unusual for languages with immutable data structures):
<
#define ORDER_PP_DEF_8hail ORDER_PP_FN( \
Line 612 ⟶ 1,473:
8seq_iota(1, 13))),
8h_loop(8S))
)</
{{out}}
<syntaxhighlight lang="text">(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)</
The C preprocessor cannot output newlines, so the output is all on one line, but easily parsable.
Line 628 ⟶ 1,489:
Next, repeatedly perform the task, until the required conditions are met, and print the counts.
<
use strict;
use warnings;
Line 669 ⟶ 1,530:
printf "%4s", ${$_->varglob('count')} for @enviornments;
print "\n";
</syntaxhighlight>
{{out}}
<pre>
Line 695 ⟶ 1,556:
</pre>
=={{header|
Emulation using edx as an "enviroment index" into static sequences. (You could of course nest the three sequences inside a single "environments" sequence, if you prefer.)<br>
See also [[Nested_function#Phix]]
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hails</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #000000;">hails</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hails</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">hails</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">results</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}):</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"==="</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
Emulation using edx as a dictionary_id (creating a separate dictionary for each environment):
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">/=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">))&</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">edx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">dicts</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"hail"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">},</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dicts</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">d</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"results"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]}):</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"==="</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"count"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dicts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
(same for both)
<pre>
1
4
2
1
=== === === === === === === === === === === ===
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
Line 746 ⟶ 1,704:
=={{header|PicoLisp}}==
Runtime environments can be controlled with the '[http://software-lab.de/doc/refJ.html#job job]' function:
<
(mapcar
'((N) (list (cons 'N N) (cons 'Cnt 0))) # Build environments
Line 765 ⟶ 1,723:
(job E
(prin (align 4 Cnt)) ) ) # print the step count
(prinl) )</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
Line 791 ⟶ 1,749:
=={{header|Python}}==
In Python, name bindings are held in dicts, one for global scope and another for local scope. When [http://docs.python.org/release/3.1.3/library/functions.html#exec exec]'ing code, you are allowed to give your own dictionaries for these scopes. In this example, two names are held in dictionaries that are used as the local scope for the evaluation of source.
<
code = '''
Line 808 ⟶ 1,766:
for env in environments:
print('% 4d' % env['cnt'], end='')
print()</
{{out}}
<pre> 1 2 3 4 5 6 7 8 9 10 11 12
Line 835 ⟶ 1,793:
=={{header|R}}==
<
if (n == 1) n else {
count <- count + 1;
Line 850 ⟶ 1,808:
cat("\nCounts:\n")
eprint(envs, "count")</
{{out}}
Line 878 ⟶ 1,836:
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 907 ⟶ 1,865:
(displayln (make-string (* 4 12) #\=))
(show-nums (get-var-values 'count))
</syntaxhighlight>
Output:
Line 934 ⟶ 1,892:
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
=={{header|Raku}}==
(formerly Perl 6)
Set up an array of hashes containing the current values and iteration counts then pass each hash in turn with a code reference to a routine to calculate the next iteration.
<syntaxhighlight lang="raku" line>my $calculator = sub ($n is rw) {
$n == 1 ?? 1 !! $n %% 2 ?? $n div 2 !! $n * 3 + 1
}
sub next (%this, &get_next) {
return %this if %this.<value> == 1;
%this.<value> .= &get_next;
%this.<count>++;
%this;
}
my @hailstones = map { %(value => $_, count => 0) }, 1 .. 12;
while not all( map { $_.<value> }, @hailstones ) == 1 {
say [~] map { $_.<value>.fmt: '%4s' }, @hailstones;
@hailstones[$_] .= &next($calculator) for ^@hailstones;
}
say "\nCounts\n" ~ [~] map { $_.<count>.fmt: '%4s' }, @hailstones;</syntaxhighlight>
{{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|REXX}}==
The formatting is
<br>
Column widths are automatically adjusted for their width (the maximum decimal digits displayed in a column).
The '''hailstone''' function (subroutine) could be coded in─line to further comply with the task's requirement that
<br>the solution have a ''single piece of code to be run repeatedly in each of these environments''.
<
parse arg
@.= /*initialize the array @. to nulls.*/
do
end /*i*/
w= length(n) /*width (so far) for columnar output.*/
do forever until @.0; @.0= 1 /* ◄─── process all the environments. */
do k=1 for
@.k= @.k x /* ◄─── where the rubber meets the road*/
end /*forever*/
#= 0 /* [↓] display the tabular results. */
do lines=-1 until _=''; _= /*process a line for each environment. */
do j=1 for n /*process each of the environments. */
select /*determine how to process the line. */
when #== 1 then _= _ right(words(@.j) - 1, w) /*environment count.*/
when lines==-1 then _= _ right(j, w) /*the title (header)*/
when lines== 0 then _= _ right('', w, "─") /*the separator line*/
otherwise _= _ right(word(@.j, lines), w)
end /*select*/
end /*j*/
if _=''
if #==1 then _= copies(" "left('',
if _\='' then say strip( substr(_, 2), "T")
end /*lines*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
hailstone: procedure expose @.; parse arg
if _==1 then return '';
{{out|output|text= when using the default input:}}
(Shown at three─fourths size.)
<pre style="font-size:75%>
1 2 3 4
── ── ── ── ── ── ── ── ── ── ── ──
1 2 3 4 5
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 40 52 10 2
══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══
0 1 7 2 5
</pre>
(Shown at two─thirds size.)
<pre style="font-size:67%;height:195ex">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ────
Line 1,118 ⟶ 2,133:
════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════
0 1 7 2 5 8 16 3 19 6 14 9 9 17 17 4 12 20 20 7 7 15 15 10 23 10 111 18 18 18 106 5 26 13 13 21 21 21 34 8 109 8 29 16 16 16 104 11 24 24 24 11 11 112 112 19 32 19 32 19
</pre>
Line 1,124 ⟶ 2,138:
{{trans|PicoLisp}}
The ''object'' is an environment for instance variables. These variables use the <code>@</code> sigil. We create 12 objects, and put <code>@n</code> and <code>@cnt</code> inside these objects. We use <code>Object#instance_eval</code> to switch the current object and bring those instance variables into scope.
<
envs = (1..12).map do |n|
Object.new.instance_eval {@n = n; @cnt = 0; self}
Line 1,152 ⟶ 2,166:
end
end
puts</
Ruby also provides the ''binding'', an environment for local variables. The problem is that local variables have lexical scope. Ruby needs the lexical scope to parse Ruby code. So, the only way to use a binding is to evaluate a string of Ruby code. We use <code>Kernel#binding</code> to create the bindings, and <code>Kernel#eval</code> to evaluate strings in these bindings. The lines between <code><<-'eos'</code> and <code>eos</code> are multi-line string literals.
<
envs = (1..12).map do |n|
e = class Object
Line 1,189 ⟶ 2,203:
eval('printf "%4s", cnt', e) # print the step count
end
puts</
{{out}}
<pre>
Line 1,212 ⟶ 2,226:
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|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">func calculator({.is_one} ) { 1 }
func calculator(n {.is_even}) { n / 2 }
func calculator(n ) { 3*n + 1 }
func succ(this {_{:value}.is_one}, _) {
return this
}
func succ(this, get_next) {
this{:value} = get_next(this{:value})
this{:count}++
return this
}
var enviornments = (1..12 -> map {|i| Hash(value => i, count => 0) });
while (!enviornments.map{ _{:value} }.all { .is_one }) {
say enviornments.map {|h| "%4s" % h{:value} }.join;
enviornments.range.each { |i|
enviornments[i] = succ(enviornments[i], calculator);
}
}
say 'Counts';
say enviornments.map{ |h| "%4s" % h{:count} }.join;</syntaxhighlight>
{{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>
Line 1,217 ⟶ 2,283:
=={{header|Tcl}}==
The simplest way to make a first-class environment in Tcl is to use a dictionary; the <code>dict with</code> command (and <code>dict update</code>; not shown here) will expand a dictionary and bind it to variables for the duration of its body script.
<
for {set i 1} {$i <= 12} {incr i} {
Line 1,247 ⟶ 2,313:
}
}
puts ""</
{{out}}
<pre>
Line 1,274 ⟶ 2,340:
</pre>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
In Wren classes satisfy the definition of first-class objects in that: "they can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable."
It also appears that they can represent first-class environments in that they can encapsulate a set of variables (the class's fields) and code (the class's methods) which acts on those variables.
So that's what we use here. However, to create a dynamic class you have to wrap it in a function which then returns a reference to the class object.
<syntaxhighlight lang="wren">import "./fmt" for Fmt
var environment = Fn.new {
class E {
construct new(value, count) {
_value = value
_count = count
}
value { _value }
count { _count }
hailstone() {
Fmt.write("$4d", _value)
if (_value == 1) return
_count = _count + 1
_value = (_value%2 == 0) ? _value/2 : 3*_value + 1
}
}
return E
}
// create and initialize the environments
var jobs = 12
var envs = List.filled(jobs, null)
for (i in 0...jobs) envs[i] = environment.call().new(i+1, 0)
System.print("Sequences:")
var done = false
while (!done) {
for (env in envs) env.hailstone()
System.print()
done = true
for (env in envs) {
if (env.value != 1) {
done = false
break
}
}
}
System.print("Counts:")
for (env in envs) Fmt.write("$4d", env.count)
System.print()</syntaxhighlight>
{{out}}
<pre>
Sequences:
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|zkl}}==
In zkl, classes wrap state. All instances of a class share code but each instance binds code to itself. In this task, class creation, instead of returning a new class instance, it returns a bound function. Calling this function calculates the next hailstone in the environment the function is bound to. To get the counts from the class/environment, we ask the function for its container and then pull the count.
<
var n,cnt=0;
fcn init(_n){n=_n; returnClass(self.f)}
Line 1,287 ⟶ 2,428:
n
}
}</
<
while(1){
ns:=es.run(True);
Line 1,295 ⟶ 2,436:
}
println("Counts:");
es.pump(String,fcn(e){"%4d".fmt(e.container.cnt)}).println();</
{{out}}
<pre>
Line 1,322 ⟶ 2,463:
{{omit from|Ada|Task could be completed, but would never truly meet definition of first class}}
{{omit from|Lily|No support for eval}}
{{omit from|Maxima}}
|