Jensen's Device: Difference between revisions

m
→‎[[Jensen's Device#python]]: partial revert, now the actual passed "i" in incremented.
m (→‎[[Jensen's Device#python]]: surprisingly python's for loop variable may be predefined. Could be useful sometimes.)
m (→‎[[Jensen's Device#python]]: partial revert, now the actual passed "i" in incremented.)
Line 70:
<pre>BEGIN
INT i;
PROC sum = (REF INT ref i, INT lo, hi, PROC REAL term)REAL:
COMMENT term is passed by-name, and so is i COMMENT
BEGIN
REAL temp := 0;
tempi := 0lo;
WHILE i <= hi DO # ALGOL 68 has a "for" loop but it creates a distinct #
FOR i FROM lo BY 1 TO hi DO
temp +:= term; # variable which would not be shared with the passed "i" #
ref i := i;
i +:= 1 # Here the actual passed "i" in incremented. #
temp := temp + term
OD;
# sum := # temp
END;
COMMENT note the correspondence between the mathematical notation and the call to sum COMMENT
print (sum (i, 1, 100, REAL: 1/i))
END</pre>
</pre>
Output: +5.18737751763962e +0
 
Line 201 ⟶ 202:
# term is passed by-name, and so is i
temp = 0
for i.value in= xrange(lo, hi+1):
while i.value <= hi: # Python has a "for" loop but it creates a distinct
temp += term()
temp += term() # variable which would not be shared with the passed "i"
i.value += 1 # Here the actual passed "i" in incremented.
return temp
# note the correspondence between the mathematical notation and the call to sum
print (sum (i, 1, 100, lambda: 1.0/i.value))</python>
</python>
Output: 5.18737751764