如何找到“环境”中的所有函数和有界符号?

发布于 2024-10-09 21:55:00 字数 285 浏览 2 评论 0原文

我正在使用一个具有内置方案解释器的软件。我知道“环境”名称是(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-10-16 21:55:00

正如 Eli Barzilay 指出的那样,您是否可以反思性地找到环境中绑定的所有名称取决于您正在使用哪种方案的实现。

我从你的问题推断你正在 MIT 方案中工作,因为你说环境的“名称”是 (the-environment)

根据我自己对 MIT 方案(版本 9.1.1)的实验,你确实可以枚举环境中绑定的绑定的名称:

1 ]=> (define (add1 n) (+ n 1))

;Value: add1

1 ]=> (add1 3)

;Value: 4

1 ]=> (environment-bound-names (the-environment))

;Value 13: (add1)

1 ]=> 

当我第一次遇到上述内容时,我有点惊讶;我原本期望在环境中看到更多的绑定,而不仅仅是我自己定义的一个绑定。

是因为您需要递归地走到父环境吗?好吧:

1 ]=> (environment-parent (the-environment))

;Value: #f

1 ]=> 

看来除了可以通过检查(the-environment)来访问的绑定之外,还必须检查通过system-global-environment可访问的绑定

1 ]=> (define global-names (environment-bound-names system-global-environment))

;Value: global-names

1 ]=> (length global-names)

;Value: 4050

1 ]=> (not (null? (memq '+ global-names)))

;Value: #t

1 ]=> (car global-names)

;Value: valid-hash-number?

:在那里,可以使用诸如环境查找之类的函数来提取每个环境中绑定的值:(

1 ]=> (environment-lookup system-global-environment '+)

;Value 14: #[arity-dispatched-procedure 14]

1 ]=> ((environment-lookup system-global-environment '+) 2 3)

;Value: 5

这可能很有用,例如,如果您想过滤全局名称> 仅列出与系统全局环境中的过程绑定的名称。)


仅供参考:我不知道上面的内容;但幸运的是,MIT Scheme 与许多其他 Lisp 方言一样,提供了 apropos 函数,当您认为自己知道要查找的函数名称的一部分时,该函数非常有用。 (apropos n) 打印出名称中包含 n 的所有绑定符号;因此,就我而言,我进行了猜测并运行 (apropos 'env) 来查看与环境相关的所有符号。该列表有点太长,无法在此处转录作为示例,但这里有类似的内容:

1 ]=> (apropos 'lookup)

#[package 14 (user)]
#[package 15 ()]
1d-table/lookup
dld-lookup-symbol
environment-lookup
environment-lookup-macro
environment-safe-lookup
hash-table/lookup
rb-tree/lookup
wt-tree/lookup
;Unspecified return value

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:

1 ]=> (define (add1 n) (+ n 1))

;Value: add1

1 ]=> (add1 3)

;Value: 4

1 ]=> (environment-bound-names (the-environment))

;Value 13: (add1)

1 ]=> 

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:

1 ]=> (environment-parent (the-environment))

;Value: #f

1 ]=> 

It seems like in addition to the bindings you can access by inspecting (the-environment), one must also inspect the bindings accessible via system-global-environment:

1 ]=> (define global-names (environment-bound-names system-global-environment))

;Value: global-names

1 ]=> (length global-names)

;Value: 4050

1 ]=> (not (null? (memq '+ global-names)))

;Value: #t

1 ]=> (car global-names)

;Value: valid-hash-number?

From there, one can use functions like environment-lookup to extract the values bound within each environment:

1 ]=> (environment-lookup system-global-environment '+)

;Value 14: #[arity-dispatched-procedure 14]

1 ]=> ((environment-lookup system-global-environment '+) 2 3)

;Value: 5

(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 have n 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:

1 ]=> (apropos 'lookup)

#[package 14 (user)]
#[package 15 ()]
1d-table/lookup
dld-lookup-symbol
environment-lookup
environment-lookup-macro
environment-safe-lookup
hash-table/lookup
rb-tree/lookup
wt-tree/lookup
;Unspecified return value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文