3

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.

1
  • 1
    You sure you don't want this? (defmacro islist[f] `(list? ~f)) Commented Dec 14, 2014 at 1:04

1 Answer 1

2

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?.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Do you happen to know if this was a conscious design choice? I was surprised when gist.github.com/karansag/2b907d0d9c570552d33f worked, so I asked this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.