Behavour of nested quotes in Scheme and Racket

人盡茶涼 提交于 2019-11-28 07:53:33

问题


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))))

prints: #t .

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

prints: quote

But

(write (quote (quote a)))

and

(write '(quote a)))

Both print: '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) .


回答1:


Putting a quote mark (') around a term and wrapping a quote form around it are identical. That is, they read to the same term.

So all of the following expressions are identical in 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". The sub-term is parenthesized, so it's a list; the inner quote is just a symbol.

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



来源:https://stackoverflow.com/questions/7984245/behavour-of-nested-quotes-in-scheme-and-racket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!