closures revisited
a closure is
- a procedure and an environment
- the environment is accessible only to the procedure
- so closure represents encapsulated state
example: bank account with one operation (withdraw)
- (define (make-withdraw bal) (lambda (i) (set! bal (- bal i)) bal))
- (define w1 (make-withdraw 100))
- (define w2 (make-withdraw 100))
- (w1 10) ==> 90
- (w1 10) ==> 80
- (w2 10) ==> 90