Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure list? and macros

Tags:

macros

clojure

I'm trying to understand how clojure macros apply to lists. I'm confused by the following:

(defmacro islist [f] (list? f))
(islist (1 2)) ; true
(islist '(1 2)) ; false
(islist (quote (1 2))) ; true

Is this expected? I've noticed that lists I pass to to macros return false when evaluated with list? inside the macro. That is, the second example is particularly confusing.

like image 759
karansag Avatar asked Apr 11 '26 10:04

karansag


1 Answers

Within the macro '(1 2) is of type clojure.lang.Cons (you can check this by changing list? to type). list? returns true iff the operand is of type clojure.lang.IPersistentList.

user=> (isa? clojure.lang.Cons clojure.lang.IPersistentList)
false

The reason clojure.lang.Cons appears is because the reader constructs a cons cell when expanding '(1 2) to (quote (1 2)), whereas it doesn't when you spell out quote directly as (quote (1 2)).

You probably want to use seq? instead of list?.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!