Clojure - Quoting Confusion

Sorry for the terribly vague name :)

I'm new to macros, and it's hard for me to understand the difference between these two statements:

`(+ 1 2 ~(+ 2 3)) ; => (clojure.core/+ 1 2 5) '(+ 1 2 ~(+ 2 3)) ; => (+ 1 2 (clojure.core/unquote (+ 2 3))) 

When I run them without unquote, do they seem pretty identical besides the qualifications?

 `(+ 1 2 (+ 2 3)) ; => (clojure.core/+ 1 2 (clojure.core/+ 2 3)) '(+ 1 2 (+ 2 3)) ; => (+ 1 2 (+ 2 3)) 

So I am mostly confused by `vs'. I understand that they both quote everything on the list, so I'm not sure why unquoting behaves differently. Basically, `behaves the way I expect both" and "will behave.

Thanks!

+7
source share
2 answers

The short answer is that unquoting only works in backquote. In the normal quoted expression, everything - including ~ and everything inside / for - has just been transmitted as is, and inside the expression with a countdown, everything inside / for ~ is evaluated (but everything else is not evaluated). So, no, not everything inside the backquoted expression remains invaluable - you can use ~ inside it to use it as a kind of template, where you "fill in the blanks" with ~ .

Edit: To indicate (pun intended) the documentation relevant to your question:

Quote:

 Quote (') 'form => (quote form) 

and (from the special forms section):

(quotation mark form) Sets an invaluable form.

 user=> '(abc) (abc) 

Please note: there is no attempt to call function a. The return value is a list of 3 characters.

Syntax quote (also called quasi-quota, backtick):

For lists / vectors / sets / maps, a syntax quote sets the template to the corresponding data structure. In a template, unqualified forms behave as if they were recursively syntactically quoted, but forms can be freed from such recursive quotation, qualifying them with inadmissibility or unquote-splicing, in which case they will be considered as expressions and they will be replaced in the template by their value or a sequence of values, respectively.

+10
source

Try to run eval based on the results of your first two expressions. The first, with `, expands to (+ 1 2 3) , which evaluates well to 6. The second, with '," extends "to (+ 1 2 (unquote (+ 1 2))) , and unquote is not valid in this context, since you are no longer inside the quote! Thus, it cannot be fully appreciated at all.

There are basically two differences between 'and `:

  • `namespace - qualifies everything
  • `resolves fuzzy
+4
source

All Articles