; 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.
Your example works, but your comment is not always true:
user=> ('+ '1)
nil
user=> ('+ '1 '2 '3)
java.lang.IllegalArgumentException: Wrong number of args passed to:
Symbol (NO_SOURCE_FILE:0)
user=> ('b '{a 10, b 11, c 12})
11
--Chouser
This is exactly right (I just confirmed it for myself). I looked up
> Looks like an if then else version of the map lookup??
> ie: (if (%1 %2) (%1 %2) %3)
> Is this a special feature of maps in general, such that you can look
> up a key but return something else if it doesn't exist?
> I hadn't come across it yet, but it sounds useful :)
the java code for Keyword.java and Symbol.java both of which implement
IFn (those things that can be put in the function position). The
single and double arity invoke() merely delegate to 'get'. And voila: