This happens because count
is creating a closure that "stores" the value of cont
, and it will remember its value even between procedure calls. Notice that the lambda
is assigned to the count
name after defining cont
as a variable, and in doing so lambda
closes over cont
. It's no longer a local variable, its a variable that's defined outside the procedure and is being shared between all of its invocations, "remembering" the value it had in the last invocation.
For comparison, look how a procedure with truly local variables looks:
(define count
(lambda ()
(let ((cont 0))
(set! cont (+ cont 1))
cont)))
The above will always return 1
, compare it with the procedure in the question.