[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic questions about elisp

From: Pascal J. Bourguignon
Subject: Re: Basic questions about elisp
Date: Sat, 07 Nov 2009 18:50:11 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

Francis Moreau <address@hidden> writes:

> On 6 nov, 22:18, address@hidden (Pascal J. Bourguignon) wrote:
>> Francis Moreau <address@hidden> writes:
>> >>> When I wrote '(2), I suppose the elisp interpreter to create a new
>> >>> list.
>> >> It does so, but at read time.  Not execution time.
>> > Ah ok I see what you mean now.
>> > That's a pretty important point, is this part covered by the elisp info ?
>> Yes.
>> > Actually the same stands for the implementation of the list, where
>> > nconc, length... are O(n). I wouldn't have thought that lists are really
>> > implemented by the car & cdr thing only.
>> Why not?  If people have been repeating for 50 years that lisp lists
>> are implemented with cons, car and cdr...
> Because I can understand there were some memory constraints 50 years
> ago that force lisp lists to be as small as possible. But I would have
> thought lisp lists (or (e)lisp) to evolve as computer memories did.

Well, contrarily to Microsoft, we don't have enough time and money to
grow a bloatware out of the language.  If lists as chains of cons
cells could be held in small memories 50 years ago, they can still be
held in big memories nowadays, there's no point in changing that.

However, if you need a "smarter" list abstract data type, you can
always implement it (or use a library).

Note however that you might have a hard time to reach the level of
efficiency given by cons cells.  That is, if you consider cons cells
as immutable, then you can easily share them amongst several prefix
lists, and therefore you spare a lot of cycles and a lot of memory not
copying them.  

If you try to invent a list ADT where you keep references to both the
first and last cells for "efficiency" reasons (so you can append an
element in O(1)), then you lose this ability to share lists, and
therefore you will have to copy these lists all over (copying the list
is O(n), and you lost the benefit of O(1) element appending).

If on the contrary you learn to use cons cell based lists, and you
avoid appending elements at the end, but push them on the head (which
is O(1)), then you can share them O(0) for no copying and spare memory
at the same time.  (Even if you need a final result in the other
order, adding a call reverse (O(n)) won't be less efficient than
making copies all the time).

__Pascal Bourguignon__

reply via email to

[Prev in Thread] Current Thread [Next in Thread]