如何在方案中定义子环境?
我只是在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相关 MIT 方案文档页面 顶级环境可能具有指导意义——您可以扩展现有的顶级环境(使用
extend-top-level-environment
)或者从头开始创建一个新环境(使用make-top-level-environment
)。不过,为了评估除了最琐碎的表达式之外的任何内容,扩展
system-global-environment
或user-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 (withmake-top-level-environment
).For evaluating anything but the most trivial expressions, though, it might be instructive to extend either
system-global-environment
oruser-initial-environment
(cf 13.2: Environment Variables)R7RS 有一个
(environment)
函数:R7RS 规范中的第 55 页
With R7RS there is an
(environment)
function:Page 55 in R7RS spec