help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: beginnerquestion (nconc)


From: Stefan Huchler
Subject: Re: beginnerquestion (nconc)
Date: Fri, 17 Mar 2017 17:59:20 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

Yuri Khan <address@hidden> writes:

> You are probably annoyed because you are familiar with list
> implementations in other languages where appending an element is a
> cheap operation. For example, with a doubly linked list, appending at
> either end is constant time.
>
> However, Lisp uses singly linked lists. Appending at the start is a
> matter of allocating a single cons cell and setting its cdr to point
> to the old head of the list. However, appending at the end requires
> traversing the whole list to find its last cell, and then adding a new
> cell there. That’s what nconc does. So if your list is 1000 items
> long, populating it from beginning to end takes roughly 500000
> operations. Populating from the end to beginning and then reversing
> will only take on the order of 3000 operations.

Hello Yuri,

thanks at least I see know WHY it is designed that way, performence.

But if I have to reverse, wouldnt it be easier that there is some sort
of:

(yreverse sequence)

that alters the sequence directly, or overwrites it.

instead of:

(setq sequence (nreverse sequence))

would make it less ugly.




reply via email to

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