简体   繁体   中英

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:

(define cond 3)

As well as 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 ?

(Feel free to edit the title, as i'm not sure how to formulate my question)

In Scheme there are no reserved identifiers. 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:

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

What sets Scheme apart from most languages is that programs are macro expanded.

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".

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

It depends on how you have implemented cond in the metacircular evaluator. Usually it checks some operators for symbols like quote and cond and then do someething special. Thus cond in operator position will be expanded as cond while cond in other circumstances would be evaluated as if it was a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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