[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: tail recursion optimization
>> From: David C. Plummer
>> Inline is orthogonal to tail recursion optimization.
>> (defun foo (a b) (declare (inline foo)) (foo a (+ a b)))
>> is equivalent to
>> (defun foo (a b) (+a (+ a (+ a (+ a ...)))))
>> and not
>> (defun foo (a b) (loop (psetq a a b (+ a b))))
I think that the wording of the manual is ambiguous enough that both
interpretations are correct. The manual only talks about the called
function being 'integrated into' the calling function. Your foo
function simply adds its arguments and then calls itself with the value
of the first argument and the sum. This is equivalent, in the
operations performed to
(defun foo (a b) (loop (psetq a a b (+ a b))))
rather than this
(defun foo (a b) (+a (+ a (+ a (+ a ...)))))
which will never end up doing even one addition.
Thus the second interpretation of 'inline foo' is, in my opinion, a
better job of integrating the definition of foo inline. The first
interpretation shows the danger of assuming that inline expansion
should be done exactly like macroexpansion.