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: David Kastrup
Subject: Re: Basic questions about elisp
Date: Thu, 05 Nov 2009 16:06:02 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

address@hidden (Pascal J. Bourguignon) writes:

> Francis Moreau <address@hidden> 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:

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.

For example, the code

(let ((lst (list 1 2 3)))
  (dotimes (i 2 lst) (nconc lst '(2))))

never finishes.  After 2 iterations, you get a circular list which hangs
the third nconc.

-- 
David Kastrup


reply via email to

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