我可以在 Scheme 的过程中定义一个全局变量吗? [英] Can I define a global from inside a procedure in Scheme?

查看:55
本文介绍了我可以在 Scheme 的过程中定义一个全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想做类似...

I have a situation where I'd like to do something like...

(define (def a b)
    (store a b) ; store the definition of 'a' somewhere
    (define-global a b)) ; also define 'a' so that its definition
                         ; is accessible later in the program

这有可能吗?据我所知 define-global 不存在,所以过程中的 define 语句只适用于本地环境.

Is this possible somehow? As far as I know define-global doesn't exist, so define statements inside procedures apply only to the local environment.

这旨在为方案中的嵌入式 DSL 创建def"过程,因此除了进行定义之外,我还需要将定义存储在我自己的符号表中.最终,我想拦截"符号查找以应用我自己的转换,返回符号查找的表达式而不是实际执行它.

This is intended for creating a 'def' procedure for an embedded DSL in scheme, so in addition to making the definition I need to store the definition in my own symbol table. Eventually I want to 'intercept' symbol look-ups to apply my own transformation, returning an expression for the symbol look-up instead of actually performing it.

我正在使用 Gambit-C 方案.

I'm using Gambit-C Scheme.

谢谢.

推荐答案

不,至少在没有任何 Gambit 特定的低级钩子的情况下.但这是有充分理由的:您的建议将导致无法进行高效编译,因为绑定可以随时更改.

No, at least not without any Gambit specific low-level hooks. But this is for a good reason: what you're suggesting would make it impossible to do efficient compilation, since bindings can change at any time.

如果您的目标是实现 DSL,那么将值保存在您自己的表中,并在您的 DSL 中实现变量查找的代码中进行查找很有意义.此外,它自然会引导您实现 DSL 存在于它自己的世界中,与实现它的代码分开.(例如,如果您有上述内容,那么当 DSL 中的代码定义一个名为 def 的变量时,您会期望发生什么?)

If your goal is to implement a DSL, then keeping the values in your own table, and doing the lookup in code that implements variable lookup in your DSL makes a lot of sense. In addition, it will naturally lead you to an implementation where the DSL lives in its own world, separately from the code that implements it. (For example, if you have the above, then what would you expect to happen when code inside the DSL defines a variable called def?)

这篇关于我可以在 Scheme 的过程中定义一个全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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