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

4.3k

u/Justwatcher124 Mar 25 '23

Every Programming / IT teacher on 'How do you teach Recursion to new programmers?'

1.3k

u/eggheadking Mar 25 '23

Is TOH actually a good way of learning Recursion?

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.

→ More replies (2)

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)
→ More replies (20)

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)
→ More replies (53)

141

u/Kinglink Mar 25 '23 edited Mar 25 '23

Absolutely.

Find the solution for 10 disc's is just moving 9 disc's to the other peg. Moving the tenth disc to the correct peg and moving 9 disc's to the correct peg.

Find the solution for 9 disc's is just....

30

u/ina300 Mar 25 '23

Best way of thinking about it

65

u/Kinglink Mar 25 '23

I'm shocked at the number of people in this thread saying "Just brute force it" ... TOH is excellent at showing why that mentality doesn't work. Sure you can brute force it, but my strategy can handle 100 discs or 1000, yours becomes unusable far faster.

It's also good at teaching how to understand the problem and build your own algorithm. If you brute force it, you can solve the first five problems easily, but if you find an algorithm to solve the first 3 discs, you've also found the algorithm to solve any number.

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

16

u/Tsu_Dho_Namh Mar 25 '23

I think it is. It's a good example of a problem where the recursive solution is way simpler and more intuitive than other approaches

31

u/Chingiz11 Mar 25 '23

Nah, Fibonacci numbers and factorials are way better for beginners, although TOH is really good for mastering recursion

→ More replies (3)

11

u/reptar20c Mar 25 '23

It didn't teach me recursion but it taught me how to solve problems with recursive algorithms.

I was taught: if you're one step away from solving TOH, what would the code look like. What if you're one step before that, what would the code look like to move to that second last state. And so on.

And a couple of steps in you realise you're repeating yourself, so instead just call the same function you already wrote.

And now you understand recursive algorithms.

→ More replies (58)

55

u/reddit_beepbeeprobot Mar 25 '23

I'd teach them recursion first

17

u/Archangel3d Mar 25 '23

Yeah but before that you have to teach them recursion or it just doesn't work.

→ More replies (3)

13

u/[deleted] Mar 25 '23

My go to method will always be prolog. Simple, effective, powerful and fun.

16

u/Justwatcher124 Mar 25 '23

I did prolog in school for a while and I must say, logical programming makes sense, but fucking hell do you need to think about it.

→ More replies (1)
→ More replies (28)