如何在方案中定义子环境? [英] How do I define a sub environment in scheme?

查看:88
本文介绍了如何在方案中定义子环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想办法使用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'.

推荐答案

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-environmentuser-initial-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)

这篇关于如何在方案中定义子环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆