T T
T T

Reputation: 261

Scheme- using set! to change a variable

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?

Upvotes: 2

Views: 1472

Answers (1)

Óscar López
Óscar López

Reputation: 236140

Your intuition is correct. This part: (let ([next 0]) is executed only once, before the lambda gets bound to the name count - that's because the first let returns as its value its last expression, which happens to be a lambda, and that's what gets bound to the variable count.

After the function definition ends, the actual execution of the count procedure starts from this line: (let ([v next]), and this happens every time the procedure is invoked. The next variable, which is the same for all the invocations of the count procedure, is accessible inside count because it was captured as part of the lambda's environment at the time of its creation, inside a closure.

Upvotes: 2

Related Questions