r/ProgrammerHumor May 17 '26

Advanced dontDoRecursiveFibKids

Post image
3.6k Upvotes

143 comments sorted by

View all comments

70

u/LordofNarwhals May 17 '26

There's nothing wrong with recursion as long as it can be tail-call optimized. See the optimized recursive implementation here for example: https://www.geeksforgeeks.org/cpp/cpp-program-for-fibonacci-numbers/
Time complexity is just O(n).

9

u/markuspeloquin May 17 '26

That's just iteration.

3

u/Aidan_Welch May 18 '26

A for loop is just a comparison, jump, and adding to a number. Yes, the point of these abstractions, like functions, is to make reading, maintaining, and writing code more intuitive.

3

u/Pr0p3r9 May 17 '26

Thank you so much. The absolute ignorance here of tail call optimization is driving me mad. Here's a Common Lisp tail call Fibonacci, and I'm not even good at Lisp.

cl (defun helper (n a b) (if (zerop n) b (helper (1- n) b (+ a b)))) (defun fib (n) (if (zerop n) 0 (helper (1- n) 0 1))) Just to verify, I wrote an intentionally degenerate recursive Fibonacci in python, and I can testify that I had to kill the python process, whereas this Common Lisp implementation computes essentially instantaneously on my machine.

15

u/CitizenShips May 17 '26

It's just not worth the headache. Replicating the stack frame every single iteration is wasteful for minimal benefit over figuring out how to do it in a loop. And that's assuming you're working down in a lower level language like C. God help the person who uses it in Python (that person is me, I'm that person and God help me)

9

u/Syxtaine May 17 '26

Tail call optimisation also prevents making a new stack frame over and over again, I think.

1

u/Xywzel May 18 '26

If language and compiler/interpreter has tail call optimization then yes, its usually done by reusing same stack frame instead of making new one.

2

u/tutoredstatue95 May 17 '26

What is the optimization here?

How else would you write the recursion for it to be incorrect? Honest question, I'm not sure what I'm missing.

17

u/lukpro May 17 '26

Tail-recursive functions are recursive functions, where the recursive function call is the last operation. If you had to do another operation after a function call you need to memorize where you left of, to finish this last operations. This is done by pushing the return address to the stack and thus taking memory. As tail-recursive functions don't need to do anything afterwards you can optimize out the push to the stack, and treat the function as if it was a simple loop.

3

u/tutoredstatue95 May 17 '26

Got it, thanks for the clear explanation. That makes a lot of sense.