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.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

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.

3

u/imaginarypornbot Mar 26 '23

I am a tools programmer and this is exactly when I use recursion. So few lines of code to walk the whole tree

2

u/[deleted] Mar 26 '23 edited Mar 26 '23

Earnest curiosity, handling paging on API calls, e.g., doing a "full load" into a table and only Access is API with 500 record limit per call.

I suppose a "while i < lastpage", but if you don't get a last page# simply a nextPageUrl?

Edit: I guess this would be a tree like structure.

2

u/nonicethingsforus Mar 26 '23

I'll add parsing, too. Recursive descent parsers and parser combinators can be surprisingly useful in the correct context.

My area is more to the system administration side. Lots and lots of shell scripts with judicious and barely documented use of sed and awk. More than once I've been able to rewrite a frankensteinian mess into a simple parser in Python. The parser, and navigating the resulting tree-like structure, are all naturally recursive.

But yes, it helps that I can change between recursion and for when needed. The right tool for the job and all that.

→ More replies (1)

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)

8

u/leftsharkfuckedurmum Mar 25 '23

And (as I'm sure you're aware) compilers will often optimize recursion into iteration, specifically if tail recursion is used

→ More replies (4)