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 18:42:55 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3.50 (windows-nt)

[Sorry for talking to myself today.]

Oliver Scholz <alkibiades@gmx.de> writes:

> Oliver Scholz <alkibiades@gmx.de> writes:
>
> [`iterate' macro with code-walker]
>>
>> So my question is: Can anybody think of a case where this approach
>> would break?
>
> Silly me. The usual case for breaking such things would apply:
>
> (iterate fact ((n 10)
>              (r 1))
>   (if (= n 1)
>       r
>     (funcall (intern "fact") (1- n) (* n r))))
>
> Not to mention `eval'.

And of course that could be circumwented by `fset'ing NAME:

(defmacro iterate (name arglist &rest body)
  (let ((catch-symbol (make-symbol "--repeat"))
        (continue (make-symbol "--continue"))
        (result (make-symbol "--result"))
        (fdef (make-symbol "--fdef"))
        (lambda-list (mapcar 'car arglist))
        (initial-args (mapcar 'cadr arglist)))
    `(let ((,fdef (and (fboundp ',name)
                           (symbol-function ',name))))
       (unwind-protect
           (let ((,continue t)
                 (,result (list ,@initial-args)))
             (fset ',name (lambda (&rest args)
                            (throw ',catch-symbol args)))
             (while ,continue
               (setq ,result
                     (catch ',catch-symbol
                       (setq ,result
                             (apply
                              (lambda ,lambda-list
                                ,@body)
                              ,result))
                       (setq ,continue nil)
                       ,result)))
             ,result)
         (if (null ,fdef)
             (fmakunbound ',name)
           (fset ',name ,fdef))))))


Yet, I dislike it more and more.


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


reply via email to

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