>>9
it isnt tail recursive because fact still has to multiply the value returned by the recursive call by x, which means the stack has to be preserved.
but if you define fact to be
(define (fact n)
(fact-iter 1 1 n))
(define (fact-iter x i n)
(if (> i n)
x
(fact-iter (* i x) (+ i 1) n)
)
)
then notice that once the recursive call returns, all fact-iter has to do is return that value. After the recursive call, fact-iter does nothing unique that it has to take up a whole stack frame. There is no reason to keep stack-iter on the call stack just to it can be immediately popped off once the recursive call returns; so it is optimized so that the the top of the call stack is replaced by the recursive call. In a sense, the interpreter is able to pop off fact-iter before it makes the recursive call to itself, this is not the case with the non-tail recursive fact, which still does work (the multiplication) after its recursive call.