When one function call is nested inside another, how does putting the outer call on hold and building a fresh scope for the inner one let you trace the whole thing down to a single value?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Write an anonymous function — a lambda — that takes one input and returns its cube
(the input raised to the third power). Assign the lambda to cube.
(Hint: ** is Python's exponent operator.)
A fresh environment for every call
State the rule that each call creates a new environment mapping formal parameters to actual arguments, then draw do_twice's environment with n bound to 3 and fn bound to lambda x: x**2.
Outer call on hold while the inner one runs
Trace the nested call through its three environments, recording that the innermost fn(n) returns 9, that 9 replaces fn(n) for the outer lambda call, which returns 81.
Resolving a name one level up
Record that the innermost lambda environment holds no n of its own and takes the value 3 from the enclosing environment when mapping x.