Difference between clojure.lang.Cons and clojure.lang.PersistenList

1,795 views
Skip to first unread message

Samuel Lê

unread,
Jan 8, 2012, 10:30:43 AM1/8/12
to clo...@googlegroups.com
Hi and Happy New Year to all the Clojure mailing list,

I am am having some trouble with the two classes Cons and PersistentList:

user> (class (conj (map #(+ % 1)  '(1 2 3)) 4))
clojure.lang.Cons
user> (class '(1 2 3 4))
clojure.lang.PersistentList

My problem is that list? returns false for a Cons and true for a PersistentList.
What can I do to make my Cons a PersistentList, or is there a way to use another function instead of list? that will return true for both classes?

Many thanks,
Sam

Cedric Greevey

unread,
Jan 8, 2012, 4:03:45 PM1/8/12
to clo...@googlegroups.com

If any (...) structure will do, test with seq? instead. If you really
need a PersistentList, you'll probably have to call that class's
constructor or resort to icky hacks like

user=> (class (apply list (cons 4 '(1 2 3))))
clojure.lang.PersistentList

(though much more evil would be

user=> (class (eval `(quote ~(cons 4 '(1 2 3)))))
clojure.lang.PersistentList

instead), because for some odd reason and despite its name (list* 4
'(1 2 3)) returns a clojure.lang.Cons.

Samuel Lê

unread,
Jan 8, 2012, 4:11:55 PM1/8/12
to clo...@googlegroups.com
Yes, seq? works. Thanks for the help!


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Cedric Greevey

unread,
Jan 9, 2012, 12:32:04 PM1/9/12
to clo...@googlegroups.com
On Sun, Jan 8, 2012 at 4:11 PM, Samuel Lê <samu...@gmail.com> wrote:
> Yes, seq? works. Thanks for the help!

You're welcome.

Reply all
Reply to author
Forward
0 new messages