3 ;; given a frame, get the value of a given variable
4 ;; false if not var is not found in the frame
5 (define (get-var-in-frame var frame)
8 ;; set a value for a given variable in a frame
9 ;; if frame already contain var, change its binding, else
10 ;; if frame does not have a binding for var, then create one.
11 (define (set-var-in-frame! var val frame)
14 ;; now using these two procedures, expressing `lookup-variable-value'
15 (define (lookup-variable-value var env)
16 (define (env-loop env)
17 (let ((frame (first-frame env)))
18 (let ((var-val (get-var-in-frame var frame)))
21 (env-loop (enclosing-environment env))))))
22 (if (eq? env the-empty-environment)
23 (error "Unbound variable" var)
27 (define (set-variable-value! var val env)
28 (define (env-loop env)
29 (let ((frame (first-frame env)))
30 (let ((var-val (get-var-in-frame var frame)))
32 (set-var-in-frame! var val frame)
33 (env-loop (enclosing-environment env))))))
34 (if (eq? env the-empty-environment)
35 (error "Unbound variable -- SET!" var)
38 (define (define-variable! var val env)
39 (let ((frame (first-frame env)))
40 (set-var-in-frame! var val frame)))