unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: David Kastrup <dak@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Basic questions about elisp
Date: Thu, 05 Nov 2009 13:59:04 +0100	[thread overview]
Message-ID: <87r5sdm8kn.fsf@lola.goethe.zz> (raw)
In-Reply-To: c4b6e407-e8f0-4d2e-8c1f-43beff08a890@r5g2000yqb.googlegroups.com

Francis Moreau <francis.moro@gmail.com> writes:

> On Nov 5, 12:50 pm, Lennart Borgman <lennart.borg...@gmail.com> wrote:
>> On Thu, Nov 5, 2009 at 12:13 PM, Francis Moreau <francis.m...@gmail.com> 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
  <do something with (car lst)>
  <maybe append something to 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
          <do something with (car iter)>
          <maybe (push something app)>
          (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


  parent reply	other threads:[~2009-11-05 12:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-05 11:13 Basic questions about elisp Francis Moreau
2009-11-05 11:50 ` Lennart Borgman
     [not found] ` <mailman.10118.1257421858.2239.help-gnu-emacs@gnu.org>
2009-11-05 12:07   ` Francis Moreau
2009-11-05 12:44     ` Lennart Borgman
2009-11-05 12:59     ` David Kastrup [this message]
2009-11-05 14:25       ` Francis Moreau
2009-11-05 14:37         ` Pascal J. Bourguignon
2009-11-05 14:58     ` Pascal J. Bourguignon
2009-11-05 12:57 ` tomas
     [not found] ` <mailman.10122.1257425638.2239.help-gnu-emacs@gnu.org>
2009-11-05 14:29   ` Francis Moreau
2009-11-05 14:41 ` Pascal J. Bourguignon
2009-11-05 15:06   ` David Kastrup
2009-11-06 16:03     ` Francis Moreau
2009-11-06 16:49       ` David Kastrup
2009-11-06 20:53         ` Francis Moreau
2009-11-06 21:18           ` Pascal J. Bourguignon
2009-11-07 14:49             ` Francis Moreau
2009-11-07 17:50               ` Pascal J. Bourguignon
2009-11-08  9:46               ` tomas
     [not found]               ` <mailman.10266.1257674088.2239.help-gnu-emacs@gnu.org>
2009-11-09 20:51                 ` Francis Moreau
2009-11-08 15:18             ` Francis Moreau
2009-11-08 16:58               ` tomas
2009-11-08 17:12               ` Pascal J. Bourguignon
2009-11-09 21:04                 ` Francis Moreau
2009-11-10 12:11                   ` Joost Kremers
2009-11-10 14:16                     ` Francis Moreau
2009-11-10 18:53                       ` David Kastrup
2009-11-06  5:06   ` Barry Margolin

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=87r5sdm8kn.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).