问题描述
我只是想办法使用Scheme(mit-scheme),而且我已经弄清楚了如何更改环境,因此'+'成为'-'运算符等效过程的符号.
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.
示例
(environment-define user-initial-environment '+ -)
(eval (+ 3 2) user-initial-environment)
=> 1
我只是想知道是否有一种简单的方法可以将环境作为变量来处理,因此当我将环境输入到eval中时,就像这样
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)
其中env是附加到我的变量'env'的一些预定义环境.
Where env is some predefined environment attached to my variable 'env'.
推荐答案
顶层环境可能具有启发性-您可以扩展现有的顶层环境(使用extend-top-level-environment
)或新建一个一个从头开始(使用make-top-level-environment
).
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
).
但是,对于评估除最琐碎的表达式以外的任何内容,扩展system-global-environment
或user-initial-environment
可能是有益的(请参阅 13.2:环境变量)
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)
这篇关于如何在方案中定义子环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!