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: Basic questions about elisp Date: Fri, 06 Nov 2009 17:49:11 +0100 Organization: Organization?!? Message-ID: <87zl6ziooo.fsf@lola.goethe.zz> References: <1e9f8449-09ec-4a84-a332-9f05fadb8aa3@z41g2000yqz.googlegroups.com> <87iqdpdog1.fsf@galatea.local> <871vkdm2p1.fsf@lola.goethe.zz> <4d5245de-4a71-4be7-a445-6d033be48490@g23g2000yqh.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 1257529538 10632 80.91.229.12 (6 Nov 2009 17:45:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 6 Nov 2009 17:45:38 +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 Nov 06 18:45:31 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 1N6SsN-0004tT-5l for geh-help-gnu-emacs@m.gmane.org; Fri, 06 Nov 2009 18:45:31 +0100 Original-Received: from localhost ([127.0.0.1]:50411 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N6SsM-0002rT-8K for geh-help-gnu-emacs@m.gmane.org; Fri, 06 Nov 2009 12:45:30 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!news2.euro.net!newsfeed.freenet.de!news.n-ix.net!noris.net!newsfeed.arcor.de!newsspool2.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:17hGWCRTojb7vgcwAxzh4UwhTzY= Original-Lines: 77 Original-NNTP-Posting-Date: 06 Nov 2009 17:49:11 CET Original-NNTP-Posting-Host: a6fa7529.newsspool2.arcor-online.net Original-X-Trace: DXC=U?WSjFPLC2h^Y=RbYBPl4`A9EHlD; 3Ycb4Fo<]lROoRa8kFbh7JP`V7ff1_LiI6ENVam3>5MOK` 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:69555 Archived-At: Francis Moreau writes: > On 5 nov, 16:06, David Kastrup wrote: >> p...@informatimago.com (Pascal J. Bourguignon) writes: >> >> >> >> > Francis Moreau writes: >> >> >> Hello, >> >> >> I'm trying to learn elisp and have a couple of basic questions. >> >> >> I'm iterating over a list using dotimes, but in the body of dotimes, >> >                                   dolist                      dolist >> >> >> the list can mutate. For example I have: >> >> >>   (dolist (elt lst) >> >>     ;; some codes >> >>     (nconc lst '(2))) >> >> > This is an infinite loop.  It will break when the program runs out of >> > memory. >> >> It was oversimplified.  But it violates one basic principle of >> programming: > > Basic principle of _elisp_ programming, I asssume... Of Lisp programming. >> Only ever use destructive list operators like nconc on lists that >> have been consed together _entirely_ under your control. >> >> In this particular case, the cons '(2) has been consed together under >> control of the Lisp reader.  The second time this code gets executed, >> the cons is destroyed. > > eh ? Did you try my example? > When I wrote '(2), I suppose the elisp interpreter to create a new > list. It does so, but at read time. Not execution time. > And doing (nconc lst '(2)), I assume that this new list is now > referenced by 'lst', hence can't be destroyed. Garbage collection is the least of your problems. > I initialy thought that this didn't work, but after retrying it, I got > the correct result: > > (let ((lst (list 1 2 3))) > (dolist (elt lst) > (when (eq elt 1) > (nconc lst '(4)))) > lst) > > which evalutes to "(1 2 3 4)" And (let ((lst (list 1 2 3))) (dolist (elt lst) (when (< elt 3) (nconc lst '(4)))) lst) just crashes. -- David Kastrup