I have a really simple piece of code I’m just trying to follow the flow of, and I think I understand but I just want to make sure
(define count
(let ([next 0])
(lambda ()
(let ([v next])
(set! next (+ next 1))
v))))
Basically, count keeps track of the number of times it has been called. When you first call it, next is set to 0 using let. And then later on, it’s change to 1 using set!. I’m trying to figure out why the (let ([next 0]) doesn’t set next back to 0 every subsequent time the program is called. From what I can tell, (lambda () …) is where the actual function starts (taking no arguments) so when you call count again, it just executes from there, using the value of next in that scope (the set! one)
I think…I’m honestly still a bit unsure. Does set! just automatically change next permanently so when you call count again, even though the first line is (let ((next 0))) it doesn’t actually go back to zero?
Your intuition is correct. This part:
(let ([next 0])is executed only once, before thelambdagets bound to the namecount– that’s because the firstletreturns as its value its last expression, which happens to be alambda, and that’s what gets bound to the variablecount.After the function definition ends, the actual execution of the
countprocedure starts from this line:(let ([v next]), and this happens every time the procedure is invoked. Thenextvariable, which is the same for all the invocations of thecountprocedure, is accessible insidecountbecause it was captured as part of thelambda‘s environment at the time of its creation, inside a closure.