r/ProgrammerHumor • • 13h ago

Meme recursion

Post image
522 Upvotes

27 comments sorted by

9

u/w1n5t0nM1k3y 7h ago

Programming courses always choose the worst examples for teaching recursion, and then they wonder why students don't understand it.

Why are they teaching us to solve fibonacci sequence with recursion when you can do it much better without. We should be teaching recursion with traversing tree structures

17

u/titpetric 12h ago

Any recursion can be flattened to a loop, thanks for coming to my TED talk

4

u/Agent_Choocho 11h ago

Trees would like to have a word

1

u/Wonderful-Habit-139 9h ago

Can they not?

1

u/Admirable_Dirt_2371 5h ago

Tell me you don't understand recursion without saying you don't understand recursion.

4

u/ReadyAndSalted 5h ago

How are they wrong?

-2

u/Admirable_Dirt_2371 4h ago

A big reason to use recursion is tail call optimization, which you lose by flattering it to a loop. Additionally recursion often works as a sort of reverse loop(the common example is a Fibonacci calculator as a recursive function) so while it could often technically get 'flattened' to a loop, it would require significantly more work.

2

u/Wonderful-Habit-139 2h ago

Bruh, tail call optimization is what allows recursion to be on the same level as a flat loop. You don't "lose" it when you flatten it, you end up not needing it in the first place.

2

u/titpetric 5h ago

What do you mean?

4

u/Agent_Choocho 11h ago

I never made it to the third panel whats the base case

3

u/Level-Pollution4993 12h ago

I dont think a recursion demonstration better than this exists.

4

u/Wonderful-Habit-139 9h ago

In what way is this recursion?

9

u/Level-Pollution4993 9h ago

Each brick is a function call, one function call invocates another function which then leads to the other and so on until the last brick; which is the base case. When the base case is reached, suddenly all functions start returning their values unwinding the call stack until the initial call completes, returning the final value; represented by the final ordered bricks.

3

u/Wonderful-Habit-139 9h ago

I stand corrected. Amazing. Take my updoots.

2

u/_Ganon 2h ago

I dont think a recursion demonstration better than this exists.

1

u/a-cox-238281 12h ago

The punchline is in the next call; this reply is waiting on itself.

1

u/MapNo2659 5h ago

Ah, recursion, the gift that keeps on giving... until it doesn't. Reminds me of the time I accidentally created an infinite loop in a script and my computer sounded like it was about to take off. Anyone else have a similar "oops" moment?

1

u/tatstytastycakes 2h ago

recursion is one of those things where you either get it and just move on with life or you dont get it and lose 6 hours of sleep

1

u/optikus 10h ago

Haha!

0

u/optikus 10h ago

Haha!

-1

u/optikus 10h ago

Haha!

-2

u/optikus 10h ago

Haha!

2

u/tatstytastycakes 2h ago

Why are people downvoting you when you're just demonstrating recursion?

1

u/optikus 1h ago

Apparently recursion jokes are a little hard to get!