When f is executed, the first thing that happens is that in f local environment creates a function g . Then the variable z is created by assignment.
Finally, x added to the result of g(x) and returned. At a point called g(x) , x = 3 and g exists in the local environment f . When a free variable z is encountered at runtime g(x) , R looks up into the next environment, causing the environment, which is the local environment f . He finds z there and continues, returning 7. Then he adds this to x , which is 3.
(Since this answer attracts more attention, I should add that my language was a little loose when I said that x βequalβ at different points, which probably do not accurately reflect R, the delayed evaluation of the arguments. x will be 3, when it is necessary.)
joran
source share