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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of recursive functions

recursive functions

Photo of Loops

Loops

Related news:

News photo

Latest Batch Of Rust Compiler Updates For GCC 15.1 Lands Support For... For Loops

News photo

First Look: Loops, by Pixelfed – Decentralised TikTok Competitor (2024)

News photo

Dragonfly's extreme loop-the-loops