Anonymous user
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
COMMENT term is passed by-name, and so is i COMMENT
BEGIN
REAL temp := 0;
WHILE i <= hi DO # ALGOL 68 has a "for" loop but it creates a distinct #
temp +:= term; # variable which would not be shared with the passed "i" #
i +:= 1 # Here the actual passed "i" in incremented. #
OD;
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>
Output: +5.18737751763962e +0
Line 201 ⟶ 202:
# term is passed by-name, and so is i
temp = 0
while i.value <= hi: # Python has a "for" loop but it creates a distinct
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>
Output: 5.18737751764
|