Jensen's Device: Difference between revisions

Content added Content deleted
(added c)
Line 171: Line 171:


i = Ref()
i = Ref()
def sum(ref_i, lo, hi, term):
def sum(i, lo, hi, term):
# term is passed by-name, and so is i
# term is passed by-name, and so is i
temp = 0;
temp = 0
for i in range(lo,hi+1):
i.value = lo
while i.value <= hi: # Python has a for loop but it creates a distinct
ref_i.value = i
temp += term()
temp += term() # variable which would not be shared with the passed i
i.value += 1
return temp
return temp
# note the correspondence between the mathematical notation and the call to sum
# note the correspondence between the mathematical notation and the call to sum