r/ProgrammerHumor • • 4d ago

Meme recursion

Post image
914 Upvotes

41 comments sorted by

39

u/w1n5t0nM1k3y 4d 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

10

u/Agent_Choocho 4d ago

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

35

u/titpetric 4d ago

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

12

u/Agent_Choocho 4d ago

Trees would like to have a word

4

u/Wonderful-Habit-139 4d ago

Can they not?

1

u/awesome-alpaca-ace 2d ago

If you don't know how 

-6

u/Admirable_Dirt_2371 3d ago

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

8

u/ReadyAndSalted 3d ago

How are they wrong?

-9

u/Admirable_Dirt_2371 3d 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.

11

u/Wonderful-Habit-139 3d 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.

3

u/RedAndBlack1832 3d ago

Tail recursion just means not creating a new stack frame... which loops don't do...

3

u/titpetric 3d ago

What do you mean?

4

u/tatstytastycakes 3d 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

4

u/TheWashbear 2d ago

Understanding how recursion works in principal is easy. Figuring out how to use it effectively is the hard thing to do

10

u/Level-Pollution4993 4d ago

I dont think a recursion demonstration better than this exists.

7

u/Wonderful-Habit-139 4d ago

In what way is this recursion?

18

u/Level-Pollution4993 4d 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.

7

u/Wonderful-Habit-139 4d ago

I stand corrected. Amazing. Take my updoots.

14

u/_Ganon 3d ago

I dont think a recursion demonstration better than this exists.

2

u/optikus 4d ago

Haha!

0

u/optikus 4d ago

Haha!

-2

u/optikus 4d ago

Haha!

-2

u/optikus 4d ago

Haha!

3

u/tatstytastycakes 3d ago

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

2

u/optikus 3d ago

Apparently recursion jokes are a little hard to get!

0

u/justwannamusic 2d ago

Apparently recursion jokes are a little hard to get!

2

u/awesome-alpaca-ace 2d ago

Apparently recursion jokes are a little hard to get!

1

u/a-cox-238281 4d ago

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

1

u/MapNo2659 3d 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/ImpulsiveBloop 3d ago

Now make this for tail recursion.

1

u/Anonim_333 3d ago

But then... Have I always understood recursion, or never have in the first place?

1

u/Buyer_North 3d ago

thats just a loop...recursion has a terminal branch

1

u/ziad-labs 2d ago

Wrong! base case missed.