Attached Quotation Behavior in Scheme and Racket

When writing a function in Racket, I accidentally put two single quotes in front of a character instead of one. that is, I accidentally wrote `` a and discovered some behavior of nested quotes, which seems strange. I use DrRacket and tested this with both Racket lang and 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 there are a couple of functions in Scheme (and Racket)? interprets (quote (quote a))) as a pair of two elements quote and a, but the write function prints' a instead of (quote a).

+6
source share
1 answer

Place a quotation mark ( ' ) around the term and wrap a quote form around it. That is, they read to the same term.

Thus, all of the following expressions are identical in the schema:

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

The quote form means "interpret what comes next as a data element, even if it contains another quote ." The subsegment is enclosed in parentheses, so it is a list; the inner quote is just a symbol.

In some cases, the printer uses abbreviations, such as the quotation mark ( ' ) in its output. I am a little surprised that you got write to do this; for me it is always written as (quote a) .

+8
source

Source: https://habr.com/ru/post/900630/


All Articles