How do I define a sub environment in scheme?

被刻印的时光 ゝ 提交于 2019-12-06 09:52:16

问题


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'.


回答1:


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)



来源:https://stackoverflow.com/questions/7111001/how-do-i-define-a-sub-environment-in-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!