How to resolve the algorithm First class environments step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm First class environments step by step in the zkl programming language

Table of Contents

Problem Statement

According to Wikipedia, "In computing, a first-class object ... is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable". 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 accessible to a statement being executed. Change the environments and the same statement could produce different results when executed. Often an environment is captured in a 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. 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.

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:

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm First class environments step by step in the zkl programming language

Source code in the zkl programming language

class Env{
   var n,cnt=0;
   fcn init(_n){n=_n; returnClass(self.f)}
   fcn f{
      if(n!=1){
         cnt += 1;
         if(n.isEven) n=n/2; else n=n*3+1;
      }
      n
   }
}

var es=(1).pump(12,List,Env);
while(1){
   ns:=es.run(True);
   ns.pump(String,"%4d".fmt).println();
   if (not ns.filter('!=(1))) break;
}
println("Counts:");
es.pump(String,fcn(e){"%4d".fmt(e.container.cnt)}).println();

  

You may also check:How to resolve the algorithm 100 doors step by step in the Frink programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Erlang programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the Sidef programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Racket programming language