4 ms·
I don't understand what his last statement means: "So please, do not call all functions where the last function call in your source code is a call to itself, t
by geuis 16y ago
I don't understand what his last statement means:
"So please, do not call all functions where the last function call in your source code is a call to itself, tail recursive. A function is tail recursive, if the last operation is a function call to itself."
It reads to me as "Don't call functions that call themselves last tail recursive. A function is tail recursive if it calls itself last."
That's saying the same thing, if my reading of it is right. His oddly-placed commas don't help.
- Jtsummers 16y agoI can't read the article (silly work web filters), but if you're quoting him correctly then I have an idea. ( function foo(n) { ... return n+foo(n-1); } is not tail recursive because the last operation is not the recursive function call, but rather the addition. function bar(n,acc) { ... return bar(n-1,acc+n); } is tail recursive because the last operation is now the recursive function call.
- akeefer 16y agoHe's pointing out that the order of statements in the source code doesn't necessary correspond to the order of evaluation. In this case even though the function call is the last statement in the source code, it's actually an argument to the multiplication, and the multiplication is the last operation being executed. That's non-obvious when just looking at the code, since the function call looks like it's last.