Here's a summary of what I think I know about quoting in Clojure. I'd
appreciate some feedback if I said anything wrong below or maybe
didn't describe something well.

; One use of quoting is to prevent a list from being evaluated
; where the first item is treated as the name of a function
; and the remaining items are treated as arguments to the function.

(println '(+ 1 2)) ; outputs (+ 1 2) instead of 3 due to the quote
(println (quote (+ 1 2))) ; same

; Quoting a list is *not* the same as quoting everyting inside it.
(println ('+ '1 '2)) ; outputs 2 which is the value of the last item evaluated

; Using a "syntax quote" produces a similar result,
; but the items are resolved to their fully-qualified names.
(println `(+ 1 2)) ; outputs (clojure.core/+ 1 2)

; In all these examples, the result can be bound to a variable
; and evaluated later.
(def demo1 '(+ 1 2))
(println (eval demo1)) ; outputs 3

; Particularly in macros it can be useful to
; evaluate a subset of the items inside a syntax quoted list.
; ~ (unquote) and ~@ (unquote-splicing) are used for this.
; ~@ splices in all the items in a collection.
; This doesn't work in quoted lists, only in syntax quoted lists.
; Note that while "quote" is the name of a real function,
; "unquote" and "unquote-splicing" are not.
(let [a 1, b [2 3]]
  (println `(+ ~a ~...@b c))) ; outputs (clojure.core/+ 1 2 3 user/c)

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to