(加载“file.scm”)在Scheme的新环境中

发布于 2024-10-24 08:10:07 字数 341 浏览 1 评论 0原文

MIT Scheme 的 (load ...) 过程 显然接受环境作为参数。有什么方法可以“克隆”当前环境并将其传递到那里,以便我可以将文件的环境与我自己的环境隔离?

(我看过这里但是我什么也没发现……)

MIT Scheme's (load ...) procedure apparently takes in an environment as a parameter. Is there any way I could "clone" the current environment and pass it there, so that I can isolate the file's environment from my own?

(I've looked here but I haven't found anything...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇俏 2024-10-31 08:10:07

像这样的事情怎么样?

(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>

How about something like this ?

(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

Edited to add:

I'm not exactly sure what you are trying to do, but here's what I did to test the above. I created a file foo.scm containing:

(set! foo 2)
(define baz (+ foo foo))
baz

Then,

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文