'(quote quote) in scheme - scheme

I'm trying to learn scheme by myself. Could anyone tell me why '(quote quote) will output 'quote, and '(quote 'quote) will output ''quote?
Thank you very much!

This expression:
'(quote quote)
... after expanding '<something> to (quote <something>) is equivalent to (quote (quote quote)), notice that the symbol quote is being quoted two times, and this expression is evaluated and printed as ''quote.
On the other hand, this expression:
'(quote 'quote)
... is equivalent to (quote (quote (quote quote))), notice that the symbol quote is being quoted three times, and this expression is evaluated and printed as '''quote.

Take a look at (free, online) How To Design Programs, intermezzo 2. It explains quote in terms of list and cons. If anything in that explanation doesn't make sense, just back up a bit in the textbook.

Related

How to output a comment using display

I'm using scheme to output some s-expressions to a port and I would really like
to add comments to the stream I'm writing.
As the ; character will comment everything after I'm unsure how to actually
do this, a comment is not an s-expression so quoting would not work
(display ;some comment) ; obviously not working
(display ';some comment) ; does not make sense
(display '(;some comment)) ; could make sense, does not work
You can escape ; using string quotes.
(display ";some comment") works.

R6RS: Does the syntax expression form applied to the empty list return a wrapped syntax object?

Consider the following expression in R6RS Scheme:
(syntax ())
When I type this expression into Racket, a (wrapped) syntax object is returned. On the other hand, the same expression yields the (unwrapped) empty list in Chez Scheme.
I am wondering which system is in conformance with the R6RS (or whether both behaviors are allowed by the standard). The relevant paragraph in R6RS is Parsing input and producing output. There, it says:
The output produced by syntax is wrapped or unwrapped according to the following rules
...
the copy of any portion of not containing any pattern variables is a wrapped syntax object.
In (syntax ()) the template does not contain any pattern variables, so it seems that the result should be a wrapped syntax object and that Racket is right.
On the other hand, R. Kent Dybvig, the author of Chez Scheme, is one of the inventors of the syntax-case system so one would expect Chez Scheme to follow the standard closely.

Why is (quote '"foo") valid syntax in Scheme?

Why does (quote '"foo") go through the Scheme interpreter? It should be syntactically redundant or wrong based on how expressions are constructed in Scheme. quote is used whenever one wants to use a symbol without Scheme thinking it's a variable and strings aren't valid symbols so why is the abbreviation for the quote operator valid when prefixed to strings? Oddly enough (quote '"foo") returns (quote "foo"). Redundancy?
Another strange experiment (symbol? '"foo") is evaluated to #f so that proves that quoted strings still aren't symbols (if the quote works that way in a statement). So, is the ' ignored on strings or does it serve some purpose elsewhere? I am using Chicken Scheme.
Somewhat trivial but kinda mind boggling at the same time.
As stated in the specification:
(quote <datum>) evaluates to the datum value represented by (see section 4.3). This notation is used to include constants.
The above doesn't exclude the quotation of strings, in fact one of the examples in that section is this one:
'"abc" => "abc"
It follows that this is also valid:
''"abc" => ''"abc"

String data from double single-quoted symbol in scheme

I read some answers here and googled, but had no luck.
I have this:
''a
in scheme (Chez scheme to be exact), and I want to turn it into a string (it's a case in my to-string lambda).
Now, asking if it's a symbol (using the symbol predicate) yields a positive answer, so I know when to operate, but I can't do anything after that since there is no way to get the inner-quote itself.
So basically I can't find a way to turn ''a into "a".
Hopefully this is simple, any help will be appreciated!
This expression:
''a
Is equivalent to this symbol definition (why the double quote, by the way? a single quote suffices):
(quote (quote a))
To turn it into a string, simply do this:
(symbol->string (cadr ''a))
=> "a"

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

Resources