如何找到“环境”中的所有函数和有界符号?
我正在使用一个具有内置方案解释器的软件。我知道“环境”名称是(the-environment)。如何找到环境中的所有函数和符号?
(define p (open-output-file "d:/test.txt"))
(display (the-environment) p)
这样可以显示所有功能吗? 提前致谢。 乔
I am using a softer who has a build-in scheme interpreter. I know the "environment" name is (the-environment). How can I find all the functions and symbols in the environment ?
(define p (open-output-file "d:/test.txt"))
(display (the-environment) p)
can this will display all the functions ?
Thanks in advance.
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Eli Barzilay 指出的那样,您是否可以反思性地找到环境中绑定的所有名称取决于您正在使用哪种方案的实现。
我从你的问题推断你正在 MIT 方案中工作,因为你说环境的“名称”是
(the-environment)
根据我自己对 MIT 方案(版本 9.1.1)的实验,你确实可以枚举环境中绑定的绑定的名称:
当我第一次遇到上述内容时,我有点惊讶;我原本期望在环境中看到更多的绑定,而不仅仅是我自己定义的一个绑定。
是因为您需要递归地走到父环境吗?好吧:
看来除了可以通过检查
(the-environment)
来访问的绑定之外,还必须检查通过system-global-environment
可访问的绑定:在那里,可以使用诸如环境查找之类的函数来提取每个环境中绑定的值:(
这可能很有用,例如,如果您想过滤全局名称> 仅列出与系统全局环境中的过程绑定的名称。)
仅供参考:我不知道上面的内容;但幸运的是,MIT Scheme 与许多其他 Lisp 方言一样,提供了 apropos 函数,当您认为自己知道要查找的函数名称的一部分时,该函数非常有用。
(apropos n)
打印出名称中包含n
的所有绑定符号;因此,就我而言,我进行了猜测并运行(apropos 'env)
来查看与环境相关的所有符号。该列表有点太长,无法在此处转录作为示例,但这里有类似的内容:As Eli Barzilay pointed out, whether or not you can reflectively find all the names bound in an environment depends on which implementation of Scheme you are using.
I infer from your question that you are working within MIT Scheme, since you said the "name" of the environment is
(the-environment)
From my own experimentation with MIT Scheme (release 9.1.1), you can indeed enumerate the names of the bindings bound in an environment:
I was a little surprised when I first encountered the above; I had expected to see many more bindings in the environment than just the single one I had defined myself.
Is it because you need to walk up to the parent environments, recursively? Well:
It seems like in addition to the bindings you can access by inspecting
(the-environment)
, one must also inspect the bindings accessible viasystem-global-environment
:From there, one can use functions like
environment-lookup
to extract the values bound within each environment:(This could be useful, for example, if you wanted to filter the
global-names
list to just the names that are bound to procedures in the system global environment.)FYI: I did not know the above off the top of my head; but luckily, MIT Scheme, like a number of other Lisp dialects, offers an
apropos
function that is very useful when you think you know part of the name of a function you are seeking.(apropos n)
prints out all the bound symbols that haven
in their name; so in my case, I made a guess and ran(apropos 'env)
to see all of the symbols that are related to environments. That list is a little too long to transcribe here as an example, but here is something similar: