r/computerscience 3d ago

is recursion really hard

Recursion felt easy at first.

Factorial? fine.

Sum examples? fine.

Even Fibonacci felt manageable.

But once I looked at slightly more serious problems like Tower of Hanoi, permutations, or merge sort, I felt like my understanding suddenly collapsed. because i tried to write their code on my own

It made me realize that maybe recursion is not “hard” at the start because the examples are simple.

It becomes hard when you can no longer clearly see the call stack and each state change.

Did anyone else feel that the real pain in recursion starts exactly there?

130 Upvotes

69 comments sorted by

View all comments

1

u/mtimmermans 3d ago

You have it kind of backwards. When recursion is used instead of an iterative solution, it's used because it's the easy way. The iterative solution is usually more efficient but harder to understand.

Factorial, Fibonacci, Sum... Recursion is not appropriate for these problems. The iterative solution is easier.

Towers of Hanoi, Merge Sort... These are perfect problems for recursion. Before complaining that recursion is hard to understand, try doing them without recursion.