How to resolve the algorithm Jensen's Device step by step in the NetRexx programming language
How to resolve the algorithm Jensen's Device step by step in the NetRexx 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 NetRexx programming language
Source code in the netrexx programming language
import COM.ibm.netrexx.process.
class JensensDevice
properties static
interpreter=NetRexxA
exp=Rexx ""
termMethod=Method
method main(x=String[]) static
say sum('i',1,100,'1/i')
method sum(i,lo,hi,term) static SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
sum=0
loop iv=lo to hi
sum=sum+termeval(i,iv,term)
end
return sum
method termeval(i,iv,e) static returns Rexx SIGNALS IOException,NoSuchMethodException,IllegalAccessException,InvocationTargetException
if e\=exp then interpreter=null
exp=e
if interpreter=null then do
termpgm='method term('i'=Rexx) static returns rexx;return' e
fw=FileWriter("termpgm.nrx")
fw.write(termpgm,0,termpgm.length)
fw.close
interpreter=NetRexxA()
interpreter.parse([String 'termpgm.nrx'],[String 'nocrossref'])
termClass=interpreter.getClassObject(null,'termpgm')
classes=[interpreter.getClassObject('netrexx.lang', 'Rexx', 0)]
termMethod=termClass.getMethod('term', classes)
end
return Rexx termMethod.invoke(null,[iv])
You may also check:How to resolve the algorithm Chaocipher step by step in the F# programming language
You may also check:How to resolve the algorithm Population count step by step in the BCPL programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the LOLCODE programming language