Clojure – difference between ‘ (apostrophe) and ` (backtick)

When you quote a collection with ', the symbol-name will be quoted exactly as you enter it.

'(+ x x) 
=> (+ x x)
(map namespace *1)
=> (nil nil nil)
'(bingo/+ lara/y user/z)
=> (bingo/+ lara/y user/z)
(map namespace *1)
=> ("bingo" "lara" "user")

When you quote a collection with the backtick, it tries to find each symbol’s namespace. If it can’t find one, it uses the current namespace. If you specify a namespace, it works the same as ' with a qualified namespace.

`(+ x x)
= > (clojure.core/+ user/x user/x)
(map namespace *1)
=> ("clojure.core" "user" "user")

When you are using ~ inside ` the form will simply be unquoted. This is helpful for building macros where the macro uses symbols from the namespace it is defined in as well as symbols from the namespace where it is used.

 `(+ ~'x x)
 => (clojure.core/+ x user/x)
 `(+ ~x x)
 => (clojure.core/+ 3 user/x)

Finally, you can unquote a whole collection of quoted things splicing.

 `(+ ~@`(x x))
 => (clojure.core/+ user/x user/x)

See both xes could have been passed as a list of namespace-qualified symbols and would have been spliced into another list. You can not use ~ or ~@ outside of a backtick-quoted collection.

Leave a Comment

techhipbettruvabetnorabahisbahis forumu