all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Basic questions about elisp
Date: Fri, 06 Nov 2009 17:49:11 +0100	[thread overview]
Message-ID: <87zl6ziooo.fsf@lola.goethe.zz> (raw)
In-Reply-To: 4d5245de-4a71-4be7-a445-6d033be48490@g23g2000yqh.googlegroups.com

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	other threads:[~2009-11-06 16:49 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
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 [this message]
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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zl6ziooo.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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.