# Questions on quoting

**URL:** <https://racket.discourse.group/t/questions-on-quoting/1472>\
**Category:** Questions & Answers\
**Tags:** quote, unquote\
**Created:** [November 14, 2022, 9:08pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472 "2022-11-14T21:08:45Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [November 14, 2022, 9:08pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/1 "2022-11-14T21:08:45Z")

</div>

Disclaimer: The following questions are _not_ relevant for any code I'm working on, but I'm curious about any answers, since I'm _still_ wondering about these questions despite telling me repeatedly they're not important. 😄

- I can use `quote` or `'` to "nest" quoting as often as I want to, as in

- I occasionally read the statement that quoting a literal gives the literal itself. Looking at

Can anyone shed some light on these two issues?

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [November 14, 2022, 9:31pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/2 "2022-11-14T21:31:15Z")

</div>

1. I think `eval` is precisely the opposite of `quote`: `quote` _prevents_ evaluation. The natural counterpart is to force evaluation using `eval` (though note considerations of `current-namespace`/the namespace argument to `eval` when in a module vs a REPL).

2. `(quote (quote 1))` evaluates to the list `(quote 1)`:

```scheme
> (equal? ''1 (list 'quote 1))
#t

```

Recall that `quote` is a special form, and as such has its own evaluation rules. Special forms are evaluated outside-in, so in `(quote_a (quote_b 1))` (marked for clarity; assume both are actually `quote`) the `quote_a` controls evaluation of the expression `(quote_b 1)` and, by the rules of `quote`, does not evaluate it but returns it directly.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [November 14, 2022, 9:33pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/3 "2022-11-14T21:33:15Z")

</div>

In fact, the latter point is what makes `(eval ''a)` work (assuming appropriate namespace setup): the `''a` is given to `eval` as `(quote a)`, and evaluating that expression is of course the result `'a`.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [November 14, 2022, 9:52pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/4 "2022-11-14T21:52:33Z")

</div>

`'<whatever>` is _read/parsed_ as `(quote <whatever>)`. For the rest of my comment, I will use `(quote <whatever>)` explicitly to make it less confusing.

`quote` is not a function. It is a form that constructs a representation of `<whatever>`. It can’t be a function because if it were, `<whatever>` would already be evaluated by the time “quote” consumes it, so it can’t access the underlying representation. This can partially explain why `(quote (quote 1))` doesn't necessarily need to be equal to `(quote 1)`.

In Racket, some expressions have a representation that coincides with itself. Numbers, like `1`, are one of these expressions. That’s why `(quote 1)` evaluates to `1`.

On the other hand, `(quote 1)` is not self-quoting. `(quote (quote 1))` needs to construct a representation of `(quote 1)`, so it constructs `(list (quote quote) 1)`.

How do you remove the outer `quote` when it’s nested in more than one level? `(quote (quote <whatever>))` evaluates to `(list (quote quote) (quote <whatever>))`. As you want `(quote <whatever>)`, you can simply access the second element.

```scheme
> (quote (quote (hello world)))
''(hello world)
> (second (quote (quote (hello world))))
'(hello world)

```

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 14, 2022, 10:14pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/5 "2022-11-14T22:14:33Z")

</div>

Total agreement with @sorawee . To add to it: I think the printed representation

```scheme
'''a

```

... is in many ways confusing and broken. It suggests that the symbol is somehow "triply quoted". That's basically not true; the outer quote is essentially "disabling" all of the internal quotes. I think it would be probably better to print this value as

```scheme
'(quote (quote a))

```

... which is another way of writing the same value. More generally, I think the printer would be less confusing if it didn't "compress" quotes occurring inside of already-quoted expressions.

The counter-argument, I suppose, would be for values like

```scheme
'(define (f x) (cons 'a x))

```

... which would then be printed as

```scheme
'(define (f x) (cons (quote a) x))

```

Thoughts?

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [November 14, 2022, 10:18pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/6 "2022-11-14T22:18:26Z")

</div>

> [@sorawee](#):
>
> `'<whatever>` is _read/parsed_ as `(quote <whatever>)`.

[sschwarzer](https://racket.discourse.group/u/sschwarzer):

> I wonder if there's also a way to "unquote" a quoted expression like this:

Since `'<whatever>` is read as `(quote <whatever>)` your "unquote" function is simply `cadr`.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 15, 2022, 12:30am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/7 "2022-11-15T00:30:25Z")

</div>

Right, unless you try to use it on `'a`. Then you're in syntax-land.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [November 15, 2022, 1:01am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/8 "2022-11-15T01:01:03Z")

</div>

You are right - I didn’t think this through. Only syntax transfers get the the reader result directly.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 15, 2022, 1:29am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/9 "2022-11-15T01:29:47Z")

</div>

Here's a "dequote". @sschwarzer , is this what you're looking for?

```scheme
#lang racket

(define-syntax dequote
  (syntax-rules (#%quote)
    [(_ (_ datum)) datum]))

(define a 1234)

(dequote '''a) ;; yields ''a
(dequote ''a) ;; yields 'a
(dequote 'a) ;; yields 1234

```

---

<div class="post-metadata">

**Author:** ![joskoot](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joskoot/32/1964_2.png) [@joskoot](https://racket.discourse.group/u/joskoot)\
**Post date:** [November 15, 2022, 5:51am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/10 "2022-11-15T05:51:48Z")

</div>

A similar problem arises when redefining quasiquote, unquote and unquote-splicing in a quoted let-form:

```scheme

(define expr

(quote

(let ((quasiquote 1) (unquote 2) (unquote-splicing 3))

(+ quasiquote unquote unquote-splicing))))

(eval expr (make-base-namespace)) à 6 ok

expr à prints: (let (`1 ,2 ,@3) (+ quasiquote . ,unquote-splicing)), unreadable.

```

The outer regular quote does not inhibit reader abbreviation for quasiquote and acompanions.

 ![DC369554041A4649A528B20B10730B39.png](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/2ad4c25f471d2cfc2782215e9bb5a535b2545175.png)

---

<div class="post-metadata">

**Author:** ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)\
**Post date:** [November 15, 2022, 10:42am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/11 "2022-11-15T10:42:45Z")

</div>

not unrelated: [99 ways to say '(I love you) in Racket](https://matt.might.net/articles/i-love-you-in-racket/)

also - for laughs; [https://twitter.com/BotRacket](https://twitter.com/BotRacket)

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/9f0a5f8ca9a3f8b265a14b8c576d5f569b300b36.png)

source: [https://cheapbotsdonequick.com/source/BotRacket](https://cheapbotsdonequick.com/source/BotRacket)

s.

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [November 15, 2022, 4:57pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/12 "2022-11-15T16:57:58Z")

</div>

Thanks a lot for all your replies! 🙂 It's much clearer now.

@sorawee I knew that `quote` was a form, not a function, but due to the printing confusion that @jbclements mentioned, I somehow thought that `quote`, applied to an inner `quote` would handle the inner `quote` in some special way and I couldn't understand how exactly. Now I understand that the inner `quote` is just treated like any other "word" and becomes a symbol (i.e. `'quote` = `(quote quote)`).

Having both representations, `'foo` and `(quote foo)` can make things more difficult to follow, but I do understand that `'foo` is more practical in a lot of cases. (I also use things like `'foo` and `'(1 2 3)` all the time.)

@sorawee, along these lines, I think it was a good idea to explain everything in terms of `quote` instead of also using `'`. 👍

> [@jbclements](#):
>
> I think the printed representation
> 
> ```scheme
> '''a
> 
> ```
> 
> ... is in many ways confusing and broken. It suggests that the symbol is somehow "triply quoted".

Indeed, I guess that was the source of my confusion.

> [@jbclements](#):
>
> Here's a "dequote". @sschwarzer , is this what you're looking for?

Yes.

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [November 16, 2022, 4:28pm UTC](https://racket.discourse.group/t/questions-on-quoting/1472/13 "2022-11-16T16:28:56Z")

</div>

In my early days with Racket, I managed to stumble into a situation like:

```scheme
#lang racket/base
(define quote "To be, or not to be.")
;; many other lines of code to make this more obscure
'foo

```

which leads to an error message that... didn't exactly clear things up for me:

```scheme
; /var/tmp/quote.rkt:7:1: foo: unbound identifier
; in: foo

```

Like anything initially imported by the `#lang` (whether function or syntax) `quote` can be redefined. Add on the `'` reader shorthand, and you can get enough layers to make a fine confusion.

* * *

I seem to recall an even more mysterious situation where I redefined something that caused input to the REPL to hang? Alas I can't remember now, exactly; it's probably somewhere on the old mailing list archives.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [November 17, 2022, 2:18am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/14 "2022-11-17T02:18:39Z")

</div>

I think there’s a slight typo in @jbclements’s program. `(dequote (1 2))` probably should not expand to `2`.

Here’s a corrected version:

```scheme
(define-syntax dequote
  (syntax-rules (quote)
    [(_ (quote datum)) datum]))

```

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [November 17, 2022, 6:25am UTC](https://racket.discourse.group/t/questions-on-quoting/1472/15 "2022-11-17T06:25:22Z")

</div>

Yeah, you're right, I got lazy, I couldn't be bothered to figure out what identifier would be left there. Should probably have realized that it's "quote", though :).

Ooh, but what if you stack your dequotes? Okay, actually I have work to do....
