简体   繁体   English

Scheme 和 Racket 中嵌套引号的行为

[英]Behavour of nested quotes in Scheme and Racket

While writing a function in Racket I accidently put two single quotes in front of a symbol instead of one.在 Racket 中编写函数时,我不小心在符号前放置了两个单引号而不是一个。 ie I accidently wrote ''a and discovered some behaviour of nested quotes that seems strange.即我不小心写了 ''a 并发现了一些看起来很奇怪的嵌套引号的行为。 I'm using DrRacket and tested this with both the Racket lang and the R5RS lang.我正在使用 DrRacket 并使用 Racket lang 和 R5RS lang 对此进行了测试。

(write (pair? (quote (quote a))))

prints: #t .打印:#t。

(write (car (quote (quote a))))

prints: quote印刷品:报价

But

(write (quote (quote a)))

and

(write '(quote a)))

Both print: 'a两者都打印:'a

Can someone tell me why in Scheme (and Racket) the function pair?有人能告诉我为什么在 Scheme(和 Racket)中使用函数对吗? interprets (quote (quote a))) as a pair of two elements quote and a , but the function write prints out 'a instead of (quote a) .将 (quote (quote a))) 解释为一对两个元素 quote 和 a ,但函数 write 打印出 'a 而不是 (quote a) 。

Putting a quote mark ( ' ) around a term and wrapping a quote form around it are identical.在术语周围加上引号 ( ' ) 和用quote括起来是相同的。 That is, they read to the same term.也就是说,他们read同一个词。

So all of the following expressions are identical in Scheme:所以以下所有表达式在 Scheme 中都是相同的:

''a
'(quote a)
(quote 'a)
(quote (quote a))

The quote form means "interpret what comes next as a datum---even if it contains another quote ". quote形式的意思是“将接下来的内容解释为数据——即使它包含另一个quote ”。 The sub-term is parenthesized, so it's a list;子项被括号括起来,所以它是一个列表; the inner quote is just a symbol.quote只是一个符号。

In some cases, the printer uses reader-abbreviations like the quote mark ( ' ) in its output.在某些情况下,打印机在其输出中使用诸如引号 ( ' ) 之类的阅读器缩写。 I'm a little surprised that you got write to do it, though;不过,我对你write来做这件事感到有些惊讶; for me, it always writes as (quote a) .对我来说,它总是写为(quote a)

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

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