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: Fri, 06 Nov 2009 17:49:11 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

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

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

Of Lisp 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.
>
> eh ?

Did you try my example?

> When I wrote '(2), I suppose the elisp interpreter to create a new
> list.

It does so, but at read time.  Not execution time.

> And doing (nconc lst '(2)), I assume that this new list is now
> referenced by 'lst', hence can't be destroyed.

Garbage collection is the least of your problems.

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

And

(let ((lst (list 1 2 3)))
   (dolist (elt lst)
     (when (< elt 3)
       (nconc lst '(4))))
   lst)

just crashes.


-- 
David Kastrup


reply via email to

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