From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.help Subject: Re: Adding many elements to a list Date: Fri, 18 Sep 2009 15:52:40 +0200 Organization: Organization?!? Message-ID: <874or0z7fb.fsf@lola.goethe.zz> References: <979e809f-0b95-46bf-afa4-1a25cda5bae0@j9g2000vbp.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1253284949 11971 80.91.229.12 (18 Sep 2009 14:42:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 18 Sep 2009 14:42:29 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Sep 18 16:42:22 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MoefF-0000zU-Dd for geh-help-gnu-emacs@m.gmane.org; Fri, 18 Sep 2009 16:42:21 +0200 Original-Received: from localhost ([127.0.0.1]:54737 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MoefE-0002YN-M6 for geh-help-gnu-emacs@m.gmane.org; Fri, 18 Sep 2009 10:42:20 -0400 Original-Path: news.stanford.edu!usenet.stanford.edu!syros.belnet.be!news.belnet.be!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help X-Face: 2FEFf>]>q>2iw=B6, xrUubRI>pR&Ml9=ao@P@i)L:\urd*t9M~y1^:+Y]'C0~{mAl`oQuAl \!3KEIp?*w`|bL5qr,H)LFO6Q=qx~iH4DN; i"; /yuIsqbLLCh/!U#X[S~(5eZ41to5f%E@'ELIi$t^ Vc\LWP@J5p^rst0+('>Er0=^1{]M9!p?&:\z]|;&=NP3AhB!B_bi^]Pfkw User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) Cancel-Lock: sha1:lMpVSucNqcDJT32i5eLzJelgass= Original-Lines: 46 Original-NNTP-Posting-Date: 18 Sep 2009 15:52:42 CEST Original-NNTP-Posting-Host: e0956c53.newsspool3.arcor-online.net Original-X-Trace: DXC=; B5M]j5lcRUE47KDAk81NWMcF=Q^Z^V3X4Fo<]lROoRQ^YC2XCjHcbYe?K6cX5S@E\CV`H8_`hhQT^9QSCVg3dOVgcO2@_RHd:TX[]o]In?81P; fXT4<4I:=V Original-X-Complaints-To: usenet-abuse@arcor.de Original-Xref: news.stanford.edu gnu.emacs.help:173151 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:68274 Archived-At: Nordlöw 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