|
|
|
|
|
by taeric
3564 days ago
|
|
I feel that you are working with a different version of "tail recursive" than any I have heard before. Specifically, it is certainly possible to make a non-tail recursive implementation of fib in Scheme. Curious why you seem to be implying otherwise. |
|
Let's look at Factorial in Scheme. You mentioned Fib, but Factorial is a simpler example, and will suffice.
The Scheme compiler/interpreter will detect that the last expression to be evaluated, and thus the return value will be: Now, you might think that Scheme would detect this as ineligible for TCO, but that's not what happens. In fact, what happens (in pseudo-asm) is this: obviously, in a real asm, sub and mul are primitives, not funcalls, but you get the idea.If that didn't make sense, just read, "LAMBDA: The Ultimate Declarative." it explains it better than I could.
So yes, all functions in Scheme are TCOed (at least, in the original implementation), it's just that some of the build of stack frames. I don't know if that's how other languages work, but it might be.