Cons vs List vs. ISeqs

194 views
Skip to first unread message

Tim Robinson

unread,
Jun 24, 2011, 7:35:20 PM6/24/11
to Clojure
I'm under the impression that traditional lisps have a greater
distinction between a cons operation vs. a list operation.
Specifically I had believed that consing was a more efficient and
better performing operation than using list.

Is this true? and if so, given both the Cons and Lists are actually
both just seqs in Clojure, does the above statement still hold true in
the Clojure world?

Thanks
Tim

Tamreen Khan

unread,
Jun 24, 2011, 9:53:15 PM6/24/11
to clo...@googlegroups.com
This stackoverflow page seems to have some info that might help: http://stackoverflow.com/questions/3008411/clojure-seq-cons-vs-list-conj

Source for PersistentList: https://github.com/hiredman/clojure/blob/master/src/jvm/clojure/lang/PersistentList.java

From looking at the source it looks like one major difference between Cons and PersistentList is that Cons can have anything that implements ISeq for _rest while PersistentList must have another PersistentList.

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

Sean Corfield

unread,
Jun 24, 2011, 10:27:14 PM6/24/11
to clo...@googlegroups.com
It might be better to link to the main Clojure github repo for viewing
the source in case things have changed since some random fork...

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Cons.java
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentList.java

David Sletten

unread,
Jun 24, 2011, 10:29:08 PM6/24/11
to clo...@googlegroups.com

On Jun 24, 2011, at 7:35 PM, Tim Robinson wrote:

> I'm under the impression that traditional lisps have a greater
> distinction between a cons operation vs. a list operation.
> Specifically I had believed that consing was a more efficient and
> better performing operation than using list.
>

This is not true, but it is also not relevant. In historical Lisps, the list datatype is a singly-linked list consisting of nodes known as CONS cells. In Common Lisp, for example, the predicate (listp obj) is simply equivalent to the test (typep obj '(or cons null)). A list is either a (chain of) CONS or the empty list. Notice that this implicitly includes "improper" lists (dotted pairs) such as (cons 1 2). In Clojure, on the other hand, it is illegal for the second argument to 'cons' to be an atom:
(cons 1 2) =>
java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Integer

A Lisp form such as (list 1 2 3) is just a series of calls to cons: (cons 1 (cons 2 (cons 3 '()))).

> Is this true? and if so, given both the Cons and Lists are actually
> both just seqs in Clojure, does the above statement still hold true in
> the Clojure world?
>

As I mentioned, the correspondence between LIST and CONS in traditional Lisps is not really relevant in Clojure, where the emphasis is on the sequence abstraction. A sequence simply satisfies an interface that provides a 'first' element, the 'rest' of the sequence, and allows you to construct ('cons') a new sequence from an existing one. Lists and vectors are two concrete sequence types, and they have significant differences in terms of behavior and performance. But in Clojure you can 'cons' using a list or a vector. So the rules are a little different from other Lisps.


Have all good days,
David Sletten


Base

unread,
Jun 25, 2011, 12:21:30 AM6/25/11
to Clojure

I agree with David when he says that this is not really relevant. As
a relative newbie to Lisps I have found that Clojure, while very Lispy
in so many ways, is actually more different than I initially thought.
It is really a hybrid of many languages wrapped in s expressions,
which is what makes it so cool!

Tim Robinson

unread,
Jun 25, 2011, 12:55:18 AM6/25/11
to Clojure
Makes sense - Thanks (to all)!
Tim
Reply all
Reply to author
Forward
0 new messages