can someone explain to me quoting (' or `) unquoting (,) and @ in scheme, drracket and how to use it? Because in my lessons it is not well explained and the examples are not concrete.
-
Read ``Quasiquotation in Lisp`` by A Bawden – alinsoar Aug 12 '21 at 11:27
-
Does this answer your question? [backquote, unquote and unquote-splicing in normal functions](https://stackoverflow.com/questions/48612047/backquote-unquote-and-unquote-splicing-in-normal-functions) – Eggcellentos Sep 05 '21 at 15:22
2 Answers
A single quote followed by the written representation of a value will produce that value:
Example:
'(1 x "foo")
will produce a value that prints as (1 x "foo")
.
Suppose now that I don't want a literal symbol x
in the list.
I have a variable x
in my program, and I want to insert
the value to which x
is bound.
To mark that I want the value of x
rather than the symbol x
,
I insert a comma before x
:
'(1 ,x "foo")
It won't work as-is though - I now get a value that has a literal comma as well as a symbol x
. The problem is that quote
does not know about the comma convention.
Backtick or quasiquote
knows about the comma-convention, so that will give the correct result:
> `(1 ,x "foo")
(1 3 "foo") ; if the value of x is 3
Read more here: https://docs.racket-lang.org/reference/quasiquote.html
Use ,@
instead of plain ,
when you want to insert the elements of a list (note that ,
will insert the list).
Compare:
> (define xs '(1 2 3))
> `(a b ,xs c d)
'(a b (1 2 3) c d)
> `(a b ,@xs c d)
'(a b 1 2 3 c d)
The syntax with the `',@
are just simplification for the following:
'expression
is the same as(quote expression)
`expression
is the same as(quasiquote expression)
,expression
is the same as(unquote expression)
,@expression
is the same as(unquote-splicing expression)
the special chars instead of the forms are translated at read time. Eg. the evaluation/scheme system never actually sees the characters, only the forms they are the same as. unquote
and unquote-splice
can only be used in a quasiquote
form. Used outside will signal an error as it doesn't have any meaning.
So here are the examples on how they work:
(define a '(1 2 3)) ; a is bound to (1 2 3)
;; standard quote. evalates verbatim to the argument
'(a ,a)
(quote (a (unquote a)))
; ==> (a (unquote a))
;; quasiquote will keep the whole structure except
;; evaluate and insert unquote parts
`(a ,a)
(quasiquote (a (unquote a)))
; ==> (a (1 2 3))
;; unqote-splicing expects the unquoted expression to evaluate
;; to a list such that it can be added as elements in the same
;; structure as the expression
`(a ,@a)
(quasiquote (a (unquote-splicing a)))
; ==> (a 1 2 3)
An yes, a REPL might translate back to the syntax. It will never show it in different ways so if your implementation displays (quote (quasiquote x))
as `x
then it will simplify all the other ones as well. How they are printed is implementation dependent.

- 47,942
- 4
- 47
- 79