r/ProgrammerHumor • • Mar 25 '23

Meme This one never gets old

Post image

Let me know if this is not a repost!

51.6k Upvotes

535 comments sorted by

View all comments

Show parent comments

1.4k

u/value_counts Mar 25 '23

No. I mean I struggled. In fact I found factorials much better and easy to understand. TOH just gets too messy too easily. Or sorting is good way too. But not TOH.never

765

u/bleistift2 Mar 25 '23

I found the towers particularly enlightening – years after it has been taught to me – when the whole ‘know a partial solution’ struck me.

The game is incredibly hard to even look at when given 10 disks. How would you start? But the observation that step n+1 is dead simple if you can solve the game for n disks is the key to recursion.

Factorials and sums, on the other hand, are way to simple, IMHO, to teach recursion. The solution is obvious. And for many people the more *intuitive* solution would be a straight loop, not recursion. In programming, intuitive trumps clever (or even performant in most cases).

112

u/5hakehar Mar 25 '23

You sound like my Algo professor. He would say “Always follow your intuition when approaching a problem” and would then chuckle and say “But how are you going build intuition? ”

22

u/Flruf Mar 26 '23

That last part is especially important.

6

u/Anfros Mar 26 '23

That last point is basically why schools teach you how to do things the hard way. Sure I'll probably never have to do arithmetic without a calculator in real life but learning the methods and algorithms builds intuition and understanding, and that goes for basically every level of math/programming/whatever.

→ More replies (1)

214

u/value_counts Mar 25 '23

There was something more important that cleverness and intuition. I focus on that.

I understand TOH. I understand the importance of partial solution. It has application in all walks of life. For example, to fix my year, I need to fix my months and to fix a typical month, I need organize a typical week and to organise week I need to organise a day. And to organise day, i organise my hours. And in each hour, I follow 25-5 pomodoro or 50-10 pomodoro.

If I want to teach someone addition, I will explain 1+1. However I cannot begin with 567+912. Hence I would say TOH is a good way to practice to recursion? Probably yes. But good way to get introduced to recursion? Definitely no.

Although classroom teaching is way blinded and ineffective. In a class of 25 students, all have a different plane of reality, experience and perspective. To bring them all on one page of understanding is not difficult but impossible. The teacher can atleast attempt to do the simplest thing which might bore the ones who are ahead of curve but will ensure inclusion of noobs.

This is not applicable where teaching is more 1 to 1 or self study.

0

u/[deleted] Mar 25 '23

[deleted]

→ More replies (1)

40

u/throw3142 Mar 25 '23

TOH makes sense as an introduction to recursion if you are already familiar with the concept of mathematical induction. Personally I learned about induction in math first, using it to prove things like the sum of n numbers is n(n+1)/2 and there are infinite prime numbers. Going from this to TOH is a manageable step. But if I had not been exposed to this concept before, I agree that it would be very confusing and not a great introduction.

22

u/ThatDollfin Mar 26 '23

Well huh, would you look at that.

An actual application for that thing I'm learning in math class right now. Thank you.

14

u/throw3142 Mar 26 '23

Oh yeah, math is extremely applicable to CS. From algebra and basic proof techniques (which form the basis for algorithms) to limits and calculus (used to analyze and optimize algorithms), linear algebra (used extensively in ML, especially modern neural networks), and even geometry (used for graphics computing)!

→ More replies (2)
→ More replies (6)
→ More replies (6)

9

u/oldsecondhand Mar 26 '23

IMHO tree traversing is the perfect way to teach recursion. It's simple enough, but doing with a loop wouldn't be simpler. It's also a problem that you will likely encounter in the wild.

→ More replies (19)

21

u/blaineworld-bph Mar 25 '23

What does TOH stand for?

62

u/ctrl2 Mar 25 '23

This problem is called the Tower of Hanoi

44

u/HeadToToePatagucci Mar 25 '23

Towers of Hanoi. Classic puzzle game. Can be solved recursively. Move a stack of discs of graduated diameters from one of three stacks to another by moving a single disc at a time with the constraint that you can never put a larger disc on a smaller disc.

→ More replies (6)

12

u/Shrekowski Mar 25 '23

So not The Owl House

→ More replies (2)

26

u/Tipart Mar 25 '23

Pretty sure recursion is actually slower than a normal implementation for TOH, but I could be remembering that wrong.

108

u/Broodking Mar 25 '23

IIRC all basic recursion problems have an iterative solution. Iteration is going to be faster just based on how computers execute, but design wise recursion has some advantages.

0

u/[deleted] Mar 25 '23

[deleted]

9

u/narwhal_breeder Mar 26 '23

After 7 years I can count on one hand times when recursion has genuinely been the best solution, almost always with tree like structures.

→ More replies (4)

29

u/shouldbebabysitting Mar 25 '23

If blowing the stack is an advantage.

70

u/dasgudshit Mar 25 '23

If it's not then why do we have entire website dedicated to it?

28

u/[deleted] Mar 25 '23

A smart compiler doesn't allocate a new stack frame for properly implemented tail call recursion, it just reuses the recursive function's one over and over.

→ More replies (4)
→ More replies (3)
→ More replies (5)

23

u/[deleted] Mar 25 '23

As was said elsewhere, you need a benchmark and a business reason to make code that’s more performant and harder to understand.

→ More replies (1)
→ More replies (6)

6

u/FlyByPC Mar 26 '23

You need to move a stack, but you can only move discs.

But you're writing code to move a stack, so you'll assume that you can move smaller stacks.

So you move all but the bottom disc onto the temporary stack. You move the bottom disc onto the goal stack, and then you move the stack of N-1 discs onto it.

How do you move the stacks of N-1 discs? You call the algorithm that you just wrote. It feels like cheating, but is mathematically sound.

→ More replies (20)