Clojure list? and macros
I am trying to understand how clojure macros apply to lists. The following confuses me:
(defmacro islist [f] (list? f))
(islist (1 2)) ; true
(islist '(1 2)) ; false
(islist (quote (1 2))) ; true
Is this expected? I noticed that the lists I pass macros to return false when evaluated using list?
inside a macro. That is, the second example is particularly confusing.
source to share
There '(1 2)
is a type inside the macro clojure.lang.Cons
(you can check this by changing list?
to type
). list?
returns true if the operand is of type clojure.lang.IPersistentList
.
user=> (isa? clojure.lang.Cons clojure.lang.IPersistentList) false
The reason clojure.lang.Cons
is because the reader constructs the cons cell when expanding '(1 2)
to (quote (1 2))
, whereas it does not when you pronounce it quote
directly as (quote (1 2))
.
You might want to use seq?
instead list?
.
source to share