(define (add-binding-to-frame! var val frame)
(set! frame (cons (cons var val) frame)))
(define (extend-environment bindings base-env)
(cons bindings base-env))
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan bindings)
(cond ((null? bindings)
(env-loop (enclosing-environment env)))
((eq? var (caar bindings))
(cdar bindings))
(else
(scan (cdr bindings)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan frame))))
(env-loop env))
(define (set-variable-value! var val env)
(define (env-loop env)
(define (scan bindings)
(cond ((null? bindings)
(env-loop (enclosing-environment env)))
((eq? var (caar bindings))
(set-cdr! (car bindings) val))
(else
(scan (cdr bindings)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan fram))))
(env-loop env))
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(define (scan bindings)
(cond ((null? bindings)
(add-binding-to-frame! var val frame))
((eq? var (caar bindings))
(set-car! (car bindings) val))
(else (scan (cdr bindings)))))
(scan frame)))