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

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

Re: tail recursion hack in Emacs Lisp?


From: Oliver Scholz
Subject: Re: tail recursion hack in Emacs Lisp?
Date: Fri, 16 Jul 2004 19:37:54 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (windows-nt)

Stefan Monnier <address@hidden> writes:

>> So my question is: Can anybody think of a case where this approach
>> would break?
>
> I have another question: why is it better than CL's `do' ?

Huh?  You mean like:

(do ((n 10 (1- n))
     (r 1 (* n r)))
    ((= n 1) r))

I had no intention to suggest that it were better. iterate/named
let is just different. Personally I find `do' hard to read, but
that might be a matter of taste.

I used `fact' just because it is the classical example. I can't
think of a really good example right now, exept maybe this:

(defun my-flatten-list (lst)
  (iterate walk-list ((lst lst)
                      (acc nil)
                      (stk nil))
    (if (null lst)
        (if (null stk)
            (nreverse acc)
          (walk-list stk acc nil))
      (if (listp (car lst))
          (walk-list (car lst)
                     acc
                     (if (null (cdr lst))
                         stk
                       (cons (cdr lst)
                             stk)))
        (walk-list (cdr lst)
                   (cons (car lst) acc)
                   stk)))))

There might be better examples. To quote the CMU CL manual: "The
main advantage of using iterate over do is that iterate naturally
allows stepping to be done differently depending on conditionals
in the body of the loop."

    Oliver
-- 
29 Messidor an 212 de la Révolution
Liberté, Egalité, Fraternité!


reply via email to

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