Get the latest tech news
Why tail-recursive functions are loops
One story every computing enthusiast should hear is the lesson ofhow loops and tail-recursion are equivalent. We like recursivefunctions because they’re amen...
In general, recursive functions are slower than loops because they push stack frames: the performance of most programs today is dominated by memory reads/writes. The key is that x is being used as an accumulator, growing a partial result in a bottom-up fashion as the computation proceeds, eventually yielding the final value at the end. Instead of keeping partial results on the stack, the loop takes a constant amount of space but linear time.
Or read this on Hacker News