How to resolve the algorithm Jensen's Device step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Jensen's Device step by step in the zkl programming language

Table of Contents

Problem Statement

This task is an exercise in call by name. Jensen's Device is a computer programming technique devised by Danish computer scientist Jørn Jensen after studying the ALGOL 60 Report. The following program was proposed to illustrate the technique. It computes the 100th harmonic number: The above exploits call by name to produce the correct answer (5.187...). It depends on the assumption that an expression passed as an actual parameter to a procedure would be re-evaluated in the caller's context every time the corresponding formal parameter's value was required. If the last parameter to sum had been passed by value, and assuming the initial value of i were 1, the result would have been 100 × 1/1 = 100. Moreover, the first parameter to sum, representing the "bound" variable of the summation, must also be passed by name (or at least by reference), otherwise changes to it (made within sum) would not be visible in the caller's context when computing each of the values to be added. (On the other hand, the global variable does not have to use the same identifier, in this case i, as the formal parameter.) Donald Knuth later proposed the Man or Boy Test as a more rigorous exercise.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Jensen's Device step by step in the zkl programming language

Source code in the zkl programming language

fcn sum(ri, lo,hi, term){
   temp:=0.0; ri.set(lo);
   do{ temp+=term(ri); } while(ri.inc()
   return(temp);
}
sum(Ref(0), 1,100, fcn(ri){ 1.0/ri.value }).println();

fcn sum2(ri, lo,hi, term){
   temp:=0.0; ri.set(lo);
   do{ temp=term + temp; } while(ri.inc()
   return(temp);
}
ri:=Ref(0);
sum2(ri, 1,100, 'wrap(){ 1.0/ri.value }).println();

fcn sum3(lo,hi, term){ [lo..hi].reduce('wrap(sum,i){ sum + term(i) },0.0) }
sum3(1,100, fcn(i){ 1.0/i }).println();

  

You may also check:How to resolve the algorithm Descending primes step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Racket programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the PHP programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the PicoLisp programming language