Recently when I self-learnt MIT 6.5151 course, I have read SICP 1 to 2.1 as ps0 requires (also read 2.2.1 as CS 61A notes requires) and then Software Design for Flexibility (SDF) Prologue, chapter 1 and partly Appendix on Scheme.
I encountered this problem when reading "Appendix on Scheme":
For example, we can use set! to make a device that increments a count every time we call it:
(define (make-counter) (let ((count 0)) (lambda () (set! count (+ count 1)) count)))
Let's make two counters:
(define c1 (make-counter)) (define c2 (make-counter))
These two counters have independent local state.
This is one similar question to this but uses (count)
instead of count
:
(define (count)
(let ((cont 0))
(lambda ()
(set! cont (+ cont 1))
cont)))
Then do the following in MIT-Scheme:
1 ]=> ((count))
;Value: 1
1 ]=> ((count))
;Value: 1
Could someone tell me the difference between this and the original define count
version? Does it due to the above one will make one new instance lambda
for each call of ((make-counter))
while the original one doesn't? (Better with one reference of SICP chapters not read up to now.)
After I read SICP chapter 3.2, I understood this problem deeper. (count)
is similar to (make-withdraw 100)
and count
is same as count
in CS61A notes p51. Each (count)
will create one new env as the book says
A procedure object is applied to a set of arguments by constructing a frame, binding the formal parameters of the procedure to the arguments of the call, and then evaluating the body of the procedure in the context of the new environment constructed.