help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic questions about elisp


From: Francis Moreau
Subject: Re: Basic questions about elisp
Date: Thu, 5 Nov 2009 04:07:11 -0800 (PST)
User-agent: G2/1.0

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:
> > 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,
> > 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.
>
> > 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, is there another way to do that ?
>
> It is much easier to answer if you define exactly what you want to do
> when lst grows.

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.

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)))

Thanks


reply via email to

[Prev in Thread] Current Thread [Next in Thread]