From: David Kastrup <dak@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Adding many elements to a list
Date: Fri, 18 Sep 2009 15:52:40 +0200 [thread overview]
Message-ID: <874or0z7fb.fsf@lola.goethe.zz> (raw)
In-Reply-To: 979e809f-0b95-46bf-afa4-1a25cda5bae0@j9g2000vbp.googlegroups.com
Nordlöw <per.nordlow@gmail.com> writes:
> What is the most efficient way of performing lots of successive
> appends of elements to the same list?
>
> If we start with defining the list
>
> (setq l '(a b))
>
> then the Emacs manual says that we can use
>
> (nconc l '(c d)))
Where? That's merely a side-effect. As a stylistic rule of thumb, you
should not use nconc where concat would not also do the trick. So in
this case, the proper usage would be
(setq l (nconc l '(c d)))
However, this particular use case is almost certainly _wrong_. nconc is
a destructive operator, you should only use it on lists that were
constructed (consed) under _your_ control. In this case, '(c d) has
been constructed under the control of the Lisp reader instead. Using
nconc on it is certain to lead to trouble eventually. To wit:
(setq l '(a b))
(dotimes (i 3) (nconc l '(c d)))
*Booooom*
Can you see what happens here?
If you don't have a clue about the ramifications of nconc, don't use it.
> I have noticed that this works in all cases except when l is nil.
> To make this case work aswell we need to do use
>
> (setq l (nconc l '(c d))))
>
>
> Or can we use setcar or setcdr in a more clever way?
nil is not a cons cell. So there is no car or cdr to set.
--
David Kastrup
prev parent reply other threads:[~2009-09-18 13:52 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-18 13:15 Adding many elements to a list Nordlöw
2009-09-18 13:44 ` Pascal J. Bourguignon
2009-09-18 14:23 ` David Kastrup
2009-09-18 14:48 ` Pascal J. Bourguignon
2009-09-18 14:54 ` David Kastrup
2009-09-18 15:13 ` Pascal J. Bourguignon
2009-09-19 7:34 ` Andreas Politz
2009-09-22 7:07 ` David Kastrup
2009-09-22 11:53 ` Pascal J. Bourguignon
2009-09-22 12:16 ` David Kastrup
2009-09-22 12:37 ` Pascal J. Bourguignon
2009-09-22 15:18 ` David Kastrup
2009-09-28 20:03 ` Samuel Wales
2009-09-18 16:24 ` Thierry Volpiatto
2009-09-18 13:52 ` David Kastrup [this message]
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874or0z7fb.fsf@lola.goethe.zz \
--to=dak@gnu.org \
--cc=help-gnu-emacs@gnu.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).