如何在方案中定义子环境?

发布于 2024-11-30 01:45:04 字数 462 浏览 3 评论 0原文

我只是在使用Scheme(mit-scheme),并且我刚刚弄清楚如何更改环境,以便“+”成为“-”运算符的等效过程的符号。

示例

(environment-define user-initial-environment '+ -)
(eval (+ 3 2) user-initial-environment)
=> 1

我只是想知道是否有一种简单的方法来将环境作为变量处理,因此当我将环境输入到 eval 中时,

(eval <exp> user-initial-environment) 

我不必使用“用户初始环境”。所以我可以在不同的环境中“玩”一个函数。

(eval <exp> env) 

其中 env 是附加到我的变量“env”的一些预定义环境。

I am just hacking around with Scheme (mit-scheme) and I have just figured out how you change the environment, so that '+' becomes a symbol for the equivalent procedure of the '-' operator.

Example

(environment-define user-initial-environment '+ -)
(eval (+ 3 2) user-initial-environment)
=> 1

I was just wondering if there were a simple way to deal with environments as variables so when I input an environment into eval, like so

(eval <exp> user-initial-environment) 

I don't have to use 'user-initial-environment'. So I can 'play' with different environments for a function.

(eval <exp> env) 

Where env is some predefined environment attached to my variable 'env'.

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

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

发布评论

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

评论(2

倦话 2024-12-07 01:45:04

相关 MIT 方案文档页面 顶级环境可能具有指导意义——您可以扩展现有的顶级环境(使用extend-top-level-environment)或者从头开始创建一个新环境(使用 make-top-level-environment)。

不过,为了评估除了最琐碎的表达式之外的任何内容,扩展 system-global-environmentuser-initial-environment 可能会有所帮助(参见 13.2: 环境变量)

The relevant MIT Scheme documentation page on top-level environments could be instructive -- you can either extend an existing top-level environment (with extend-top-level-environment) or make a new one from scratch (with make-top-level-environment).

For evaluating anything but the most trivial expressions, though, it might be instructive to extend either system-global-environment or user-initial-environment (cf 13.2: Environment Variables)

失去的东西太少 2024-12-07 01:45:04

R7RS 有一个(environment) 函数:

(环境列表1 ...)
eval 库过程
此过程返回一个环境说明符
从空环境开始然后得到结果
将每个列表(被视为导入集)导入其中。
(有关导入集的说明,请参阅第 5.6 节。)
由说明符表示的环境的绑定
是不可变的,环境本身也是如此。

R7RS 规范中的第 55 页

With R7RS there is an (environment) function:

(environment list1 ...)
eval library procedure
This procedure returns a specifier for the environment that
results by starting with an empty environment and then
importing each list, considered as an import set, into it.
(See section 5.6 for a description of import sets.) The
bindings of the environment represented by the specifier
are immutable, as is the environment itself.

Page 55 in R7RS spec

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