MIT Scheme 的(load ...)
程序显然将环境作为参数。有什么办法可以“克隆”当前环境并将其传递到那里,以便我可以将文件的环境与我自己的环境隔离开来?
(我看过这里,但我没有找到任何东西......)
这样的事情怎么样?
(define (clone-env env)
(let ((bindings (environment-bindings env)))
(make-top-level-environment (map car bindings)
(map cadr bindings))))
1 ]=> (define foo 1)
;Value: foo
1 ]=> (eq? (the-environment) (clone-env (the-environment)))
;Value: #f
编辑添加:
我不确定您要做什么,但这是我为测试上述内容所做的。我创建了一个文件foo.scm
,其中包含:
(set! foo 2)
(define baz (+ foo foo))
baz
然后,
1 ]=> (define foo 1)
;Value: foo
1 ]=> (load "foo.scm" (clone-env (the-environment)))
;Loading "foo.scm"... done
;Value: 4
1 ]=> foo
;Value: 1
1 ]=> baz
;Unbound variable: baz
;To continue, call RESTART with an option number:
; (RESTART 3) => Specify a value to use instead of baz.
; (RESTART 2) => Define baz to a given value.
; (RESTART 1) => Return to read-eval-print level 1.
2 error>