all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: Stefan Huchler <stefan.huchler@mail.de>
Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: beginnerquestion (nconc)
Date: Fri, 17 Mar 2017 21:42:37 +0700	[thread overview]
Message-ID: <CAP_d_8XMuGGtuOukcb6UUvf9KpejjjP_Q1DJJr-S1mqftBpu=g@mail.gmail.com> (raw)
In-Reply-To: <87shmc1m2u.fsf@mail.de>

On Fri, Mar 17, 2017 at 12:58 PM, Stefan Huchler <stefan.huchler@mail.de> wrote:

> I am a bit anoyed by push and reverse lists its not very straightforward
> solution to create lists in loops, I found in the doku the nconc macro,
> which looks like some sort of push that puts sequences at the end
> instead of the beginning.

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.

Also, the empty list is a special case. It is represented as nil and
does not *have* a last cell that nconc could modify. That’s why nconc
does not work on the empty list.



  parent reply	other threads:[~2017-03-17 14:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-17  5:58 beginnerquestion (nconc) Stefan Huchler
2017-03-17  7:33 ` Thien-Thi Nguyen
2017-03-17  8:52   ` Thien-Thi Nguyen
2017-03-17 14:19   ` Stefan Huchler
2017-03-17 14:48     ` tomas
2017-03-17 14:42 ` Yuri Khan [this message]
2017-03-17 16:59   ` Stefan Huchler
2017-03-17 17:20     ` Drew Adams
2017-03-17 17:32       ` Drew Adams
2017-03-18  2:15     ` Stefan Monnier
2017-03-21 16:47       ` Stefan Huchler
2017-03-21 16:59       ` Stefan Huchler
2017-03-21 20:14         ` John Mastro
2017-03-22  0:32           ` Stefan Huchler
2017-03-22 15:02             ` Michael Heerdegen
2017-03-22 19:00             ` John Mastro
2017-03-21 11:05     ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAP_d_8XMuGGtuOukcb6UUvf9KpejjjP_Q1DJJr-S1mqftBpu=g@mail.gmail.com' \
    --to=yuri.v.khan@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=stefan.huchler@mail.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.