(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unkown request -- MAKE-ACCOUNT" m)))) dispatch) ; After (define acc (make-account 50)) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 50 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | | dispatch: ----> env: E1, parameters: m, body: ... | | --------------| /\ | | | | acc: ----------------------------------- | | ------------------ ; During ((acc 'deposit) 40) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 50 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | ->| dispatch: ----> env: E1, parameters: m, body: ... | | | --------------| /\ | | | /\ | | acc: --------------|--------|----------- | | | | | | | E2 | | | | --------------- | | | | m: 'deposit | | | | --------------- | | | ((acc 'deposit) 40) | | | | | | E3 | | | -------------- | | |-| amount: 40 | | | -------------- | | ------------------ ; After ((acc 'deposit) 40) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 90 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | | dispatch: ----> env: E1, parameters: m, body: ... | | --------------| /\ | | | | acc: ----------------------------------- | | ------------------ ; During ((acc 'withdraw) 60)) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 90 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | ->| dispatch: ----> env: E1, parameters: m, body: ... | | | --------------| /\ | | | /\ | | acc: --------------|--------|----------- | | | | | | | E2 | | | | --------------- | | | | m: 'deposit | | | | --------------- | | | ((acc 'withdraw) 60) | | | | | | E3 | | | -------------- | | |-| amount: 60 | | | -------------- | | ------------------ ; After ((acc 'withdraw) 60)) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 30 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | | dispatch: ----> env: E1, parameters: m, body: ... | | --------------| /\ | | | | acc: ----------------------------------- | | ------------------ ; After (define acc2 (make-withdraw 100)) global env ------------------ | |<--- env: global env | | parameters: balance | make-account: -----> body: ... | | | | E1 | | --------------- | |<----| balance: 30 | | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | | dispatch: ----> env: E1, parameters: m, body: ... | | --------------| /\ | | | | acc: ----------------------------------- | | | | E2 | | ---------------- | |<----| balance: 100 | ; point to the same lambdas as above | | | withdraw: ----> env: E1, parameters: amount, body: ... | | | deposit: -----> env: E1, parameters: amount, body: ... | | | dispatch: ----> env: E1, parameters: m, body: ... | | ---------------| /\ | | | | acc2: ----------------------------------- | | ------------------ The local state for acc is kept in the environment (E1) for the make-account call. The states for the two accounts are kept distinct because each account has a separate environment created by the distinct make-account calls. acc and acc2 share only the global environment and the function source code.