简体   繁体   English

为什么我可以定义新的条件,而Scheme不会与我的新条件条件混淆呢?

[英]Why can i define a new cond, and Scheme will not get confused with my new cond over the conditional cond?

I have this task where i'm working with a metacircular evaluator, and i define a new cond like this: 我在与一个元圆形评估器一起工作的地方执行此任务,并定义了一个新的cond如下所示:

(define cond 3)

As well as else : 以及else

(define (else x) (/ x 2)

My question is why does this (below) actually work? 我的问题是为什么这个(下面)实际上有效?

(cond ((= cond 2) 0) 
(else (else 4)))

How does Scheme know which cond is my defined cond and my else , over the conditional cond and else ? 在有条件的condelse条件下,Scheme如何知道我的定义的条件和else cond是哪个cond

(Feel free to edit the title, as i'm not sure how to formulate my question) (请随意编辑标题,因为我不确定如何提出我的问题)

In Scheme there are no reserved identifiers. 在Scheme中,没有保留的标识符。 In many languages there is a list of reserved identifiers (keywords) that can't be used as names of variables. 在许多语言中,都有不能用作变量名称的保留标识符(关键字)列表。

In Scheme you can for example do this: 例如,在Scheme中,您可以执行以下操作:

> (let ((cond +))
    (cond 1 2))
3

What sets Scheme apart from most languages is that programs are macro expanded. 使Scheme与大多数语言不同的是,程序可以进行宏扩展。

Running a Scheme program: 运行计划程序:

read -> macro expansion -> compilation -> execution

It is non-trivial to explain how the macro expansion algorithm works. 解释宏扩展算法的工作原理并非难事。 I can recommend the chapter "Syntactic Abstraction: The syntax-case expander" by R. Kent Dybvig in the book "Beautiful Code". 我可以在“ Beautiful Code”一书中推荐R. Kent Dybvig撰写的“语法抽象:语法大小写扩展器”一章。

https://www.cs.indiana.edu/~dyb/pubs/bc-syntax-case.pdf https://www.cs.indiana.edu/~dyb/pubs/bc-syntax-case.pdf

It depends on how you have implemented cond in the metacircular evaluator. 这取决于您在metacircular评估程序中如何实现cond Usually it checks some operators for symbols like quote and cond and then do someething special. 通常,它会检查一些运算符中是否存在诸如quotecond类的符号,然后执行一些特殊的操作。 Thus cond in operator position will be expanded as cond while cond in other circumstances would be evaluated as if it was a variable. 这样,操作员位置中的cond将与cond展开,而其他情况下的cond将被视为变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM