Scheme 和 Racket 中嵌套引号的行为 [英] Behavour of nested quotes in Scheme and Racket
问题描述
在 Racket 中编写函数时,我不小心在符号前加上了两个单引号而不是一个.即我不小心写了 ''a 并发现了一些看起来很奇怪的嵌套引号的行为.我正在使用 DrRacket 并使用 Racket lang 和 R5RS lang 对此进行了测试.
While writing a function in Racket I accidently put two single quotes in front of a symbol instead of one. i.e. I accidently wrote ''a and discovered some behaviour of nested quotes that seems strange. I'm using DrRacket and tested this with both the Racket lang and the R5RS lang.
(write (pair? (quote (quote a))))
打印:#t.
(write (car (quote (quote a))))
印刷品:报价
但是
(write (quote (quote a)))
和
(write '(quote a)))
两者都打印:'a
谁能告诉我为什么在Scheme(和Racket)函数对中?将 (quote (quote a))) 解释为一对两个元素 quote 和 a ,但函数 write 打印出 'a 而不是 (quote a) .
Can someone tell me why in Scheme (and Racket) the function pair? 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
形式是相同的.也就是说,他们阅读
到同一个词条.
Putting a quote mark ('
) around a term and wrapping a quote
form around it are identical. That is, they read
to the same term.
所以以下所有表达式在 Scheme 中都是相同的:
So all of the following expressions are identical in Scheme:
''a
'(quote a)
(quote 'a)
(quote (quote a))
quote
形式的意思是将接下来的内容解释为数据——即使它包含另一个quote
".子项被括号括起来,所以它是一个列表;内部 quote
只是一个符号.
The quote
form means "interpret what comes next as a datum---even if it contains another quote
". The sub-term is parenthesized, so it's a list; the inner quote
is just a symbol.
在某些情况下,打印机在其输出中使用诸如引号 ('
) 之类的阅读器缩写.不过,你让 write
来做这件事,我有点惊讶;对我来说,它总是写为 (quote a)
.
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; for me, it always writes as (quote a)
.
这篇关于Scheme 和 Racket 中嵌套引号的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!