我正在尝试根据 Harold Abelson 和 Gerald Jay Sussman 着名的“计算机程序的结构和解释”一书在 Scheme 中实现 Metacircular Evaluator。
http://mitpress.mit.edu/sicp/full-text/sicp/book/node79.html , http://mitpress.mit.edu/sicp/full-text/sicp/book/node80.html
作者建议以这种方式设置环境:
(define (define-variable! var val env)
(let ((frame (first-frame env)))
(define (scan vars vals)
(cond ((null? vars)
(add-binding-to-frame! var val frame))
((eq? var (car vars))
(set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(scan (frame-variables frame)
(frame-values frame))))
(define (setup-environment)
(let ((initial-env
(extend-environment (primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)))
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
initial-env))
但是,我不明白为什么
(define myenv (setup-environment))
应该像我们在Scheme中所期望的那样工作,因为据我所知,Scheme默认通过值传递变量来函数,因此在两次应用“define-variable!”之后 对于initial-env,initial-env不会每次都改变,setup-environment函数会返回extend-environment返回的值。
我的理解错误在哪里,请您指教?
先感谢您!