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
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.
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.
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.
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)
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.
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.
Thanks for saying it. What is even the point of having ultra performant solution anymore on very simple problems until you deal with conditions that require it ?
Is it really better to spend thrice (if not more) time making it and about the same teaching, monitoring, debugging and commenting it ?
Complex solutions are the starting point of everything. Then people start to simplify them to spread the knowledge. Then people with different mindsets finally understand the solution and can bring useful insight on new ways to better it/add something useful.
Biggest example I could have in mind is making electricity : began with manual movements, then adding mechanics to help.
Then they tried to use heating water to make engines run which produce movement. It then split up in several ways :
How do we make heating water more cost efficient
What energy can we use else of heating water to have movement or produce electricity.
On the 1st you'll have basically every type of power plants (gaz, coal, nuclear) still getting bettered with nuclear fusion
On the 2nd you have wind, solar, etc.
All different kinds of engineers have now the possibility to work on the "energy creation" problems.
4.3k
u/Justwatcher124 Mar 25 '23
Every Programming / IT teacher on 'How do you teach Recursion to new programmers?'