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

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

Re: cl-dolist, dolist, cl-return,


From: Emanuel Berg
Subject: Re: cl-dolist, dolist, cl-return,
Date: Fri, 10 Jul 2015 00:00:47 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

John Mastro <john.b.mastro@gmail.com> writes:

> Stefan means that if, given `(dolist (x (list 1 2
> 3)) ...)', you evaluated `(list 1 2 3)' on every
> iteration you would end up doing something the moral
> equivalent of
>
>     (setq elt (nth 0 (list 1 2 3))) ;; first iteration
> (setq elt (nth 1 (list 1 2 3))) ;; second iteration
>
> and so on, rather than
>
>     (let ((the-list (list 1 2 3))) (setq elt (pop
> the-list)) ;; first iteration (setq elt (pop
> the-list))) ;; second iteration
>
> The latter case (which is how things really work) is
> obviously much more efficient, because the former
> case needlessly re-walks the list.

Yes, there is no doubt in my mind the list shouldn't
be evaluated on each iteration, for I don't know how
many reasons!

The reason I asked is the C for loop, which looks like
this in C99 style:

    for (int i = 0; i < a_size; i++) { a[i] = ... ; }

But sometimes, people are tempted to do like this:

    for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
       a[i] = ... ;
    }

In the real world probably it won't matter one bit
most of the time but in the world of programming the
craftsman don't want the computation to take place at
every iteration, especially since the property
doesn't change!

If you don't have to deal with this in `dolist' I'm
happy :)

-- 
underground experts united
http://user.it.uu.se/~embe8573




reply via email to

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