Saturday, 17 August 2013

higher order function python walkthrough

higher order function python walkthrough

def tracer(fn):
def traced(x):
print('Calling',fn,'(',x,')')
result=fn(x)
print('Got',result,'from',fn,'(',x,')')
return result
return traced
def fact(n):
if n ==0:
return 1
return n * fact(n-1)
new_fact = tracer(fact)
new_fact(2)
I used this code on pythontutor.com to better understand higher order
functions, but i'm having difficulty understanding why new_fact(2) in step
8 is mapped to traced? In other words, how does the traced function know
the argument is 2?

No comments:

Post a Comment