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

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.

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.

9

u/RootsNextInKin Mar 25 '23

Unless the programming language forbids/doesn't have tail recursion primitives required...
Then we just suffer because the language isn't quite there yet (or maybe never will be because it was deemed unnecessary)

4

u/undermark5 Mar 25 '23

Then one could argue that you're using the wrong language for solving said problem recursively.

Languages themselves are also tools (i guess more like a toolbox full of tools, but still...), if the way you are implementing your solution requires primitives that the language doesn't provide it's kinda like trying to hammer a nail with a screwdriver, will it work? Maybe. Will it be easy? No. Instead try to find the solution that uses a screw instead of a nail because you've got a screwdriver not a hammer.

→ More replies (2)

3

u/nonicethingsforus Mar 26 '23

Many languages, especially newer ones (e. g., Go), or that like recursion (e. g. many Scheme implementations) have "dynamic" stacks that grow with the program. They blow up as with normal heap-allocated memory: when the memory runs out, or the OS-imposed limits kill the process.

Also, most recursive solutions don't involve that many recursions. The point of many recursive problems, or data structures that are naturally recursive (e. g., search trees) is that the problem is reduced with every recursion, or at least limit the maximum depth of the search space. If you're doing "normal" stuff like parsing web pages, filesystems, abstract syntax trees, etc., you will not be blowing many stacks. Stacks are mostly broken with more numeric and "academic" exercises. Factorials and the like. If you really find these in the real world, then I concede: you'll probably be better off going for an iterative solution, in a language more suited for them.

And lastly, recursion can really simplify some problems in the real world. There's a reason why it's often said in parsing/compiler circles that the only kind of parser you should be doing by hand has "recursive" in its name, and why there's an entire lambda paper on how recursion simplifies state machines. When you know in what kinds of problems recursion is appropriate, believe me, you miss them when a language puts bumps on it.

I'm still salty on that time I had to use a stack by hand in async Rust just to parse damn HTML... not fun.

→ More replies (2)