How to resolve the algorithm First class environments step by step in the PicoLisp programming language
How to resolve the algorithm First class environments step by step in the PicoLisp 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 PicoLisp programming language
Source code in the picolisp programming language
(let Envs
(mapcar
'((N) (list (cons 'N N) (cons 'Cnt 0))) # Build environments
(range 1 12) )
(while (find '((E) (job E (> N 1))) Envs) # Until all values are 1:
(for E Envs
(job E # Use environment 'E'
(prin (align 4 N))
(unless (= 1 N)
(inc 'Cnt) # Increment step count
(setq N
(if (bit? 1 N) # Calculate next hailstone value
(inc (* N 3))
(/ N 2) ) ) ) ) )
(prinl) )
(prinl (need 48 '=))
(for E Envs # For each environment 'E'
(job E
(prin (align 4 Cnt)) ) ) # print the step count
(prinl) )
You may also check:How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Factor programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm A+B step by step in the SPL programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the zkl programming language
You may also check:How to resolve the algorithm Read entire file step by step in the PL/I programming language