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: Thu, 05 Nov 2009 13:59:04 +0100 Organization: Organization?!? Message-ID: <87r5sdm8kn.fsf@lola.goethe.zz> References: <1e9f8449-09ec-4a84-a332-9f05fadb8aa3@z41g2000yqz.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 1257428707 23992 80.91.229.12 (5 Nov 2009 13:45:07 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 5 Nov 2009 13:45:07 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Nov 05 14:44:39 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 1N62di-0005Tb-Je for geh-help-gnu-emacs@m.gmane.org; Thu, 05 Nov 2009 14:44:38 +0100 Original-Received: from localhost ([127.0.0.1]:48476 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N62dh-0002f7-QI for geh-help-gnu-emacs@m.gmane.org; Thu, 05 Nov 2009 08:44:37 -0500 Original-Path: news.stanford.edu!usenet.stanford.edu!goblin2!goblin.stu.neva.ru!newsfeed.kpn.net!pfeed09.wxs.nl!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.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:fFLtvF0Eg3iuC6Ha3e2/f++c+pc= Original-Lines: 90 Original-NNTP-Posting-Date: 05 Nov 2009 13:59:07 CET Original-NNTP-Posting-Host: edd1ade9.newsspool1.arcor-online.net Original-X-Trace: DXC=i=g^?f=LE\kOKO]LCQ@0g`ic==]BZ:afn4Fo<]lROoRa<`=YMgDjhgb?jFa8ZHTgT`1_LiI6ENVam3>5MOK`UHCib_oAGTUe 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:69491 Archived-At: Francis Moreau writes: > On Nov 5, 12:50 pm, Lennart Borgman wrote: >> On Thu, Nov 5, 2009 at 12:13 PM, Francis Moreau wrote: >> > I'm iterating over a list using dotimes, but in the body of dotimes, >> > the list can mutate. For example I have: >> >> >  (dolist (elt lst) >> >    ;; some codes >> >    (nconc lst '(2))) >> >> > This adds/appends a new element to 'lst' list. It looks like 'dotimes' >> > doesn't like it. dolist. >> >> > So I eventually wrote it like this >> >> >    (setq i 0) >> >    (while (< i (length lst)) >> >          ;; some codes >> >          (x-nconc lst '(2)))) >> >      (setq i (1+ i))) >> >> > which is a bit ugly, This looks like you would use (elt lst i) inside of the loop, quite a bad idea since you get quadratic behavior. > I'd like to iterate over all elements of the list even if the element > are appended during the loop execution. > > For example if the list is (1 2) before entering in the loop and then > during the first iteration the code append '3' to the list so it > becomes (1 2 3), I'd like the loop to iterate over the '3' element as > well. 'dotimes' doesn't seem to do that. dolist > I noticed that I used 'x-nconc' without giving its definition: it is > something I wrote for my own need: it's the same as 'nconc' except if > the list given in parameter is nil then 'x-nconc' creates a new list > and assigns it to its first parameter. > > I did the following macro to do that, although I'm not sure it's the > good thing to do: > > (defmacro x-nconc (l e) > `(if (null ,l) (setq ,l ,e) (nconc ,l ,e))) Actually, that's pretty stupid since it is exactly the same as (defmacro (l e) `(setq ,l (nconc ,l ,e))) It is less obscure to use nconc in the same manner as append, namely using the return value (and accepting the side-effect for efficiency's sake). And then you don't need your personal macro and get more readable code. The usual iteration would be something like (while lst (setq lst (cdr lst))) If you really want efficiency, you'll not append something to lst in order to avoid quadratic behavior. Then you'd rather do something like (let ((iter lst) app) (while (progn (while iter (setq lst (cdr lst))) app) (setq iter (nreverse app) app nil))) Note that this does not change the original list at all. If that's not what is desired, you can write the last setq as (setq iter (nreverse app) lst (nconc lst iter) app nil) This still is suboptimal since the nconc will repeatedly traverse the same elements. Better solutions will track the additions also by just pushing them, and do the concatenation as the very last step. -- David Kastrup