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: Fri, 6 Nov 2009 08:03:36 -0800 (PST)
User-agent: G2/1.0

On 5 nov, 16:06, David Kastrup <d...@gnu.org> wrote:
> p...@informatimago.com (Pascal J. Bourguignon) writes:
>
>
>
> > Francis Moreau <francis.m...@gmail.com> 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...

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

When I wrote '(2), I suppose the elisp interpreter to create a new
list. And doing (nconc lst '(2)), I assume that this new list is now
referenced by 'lst', hence can't be destroyed.

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


reply via email to

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