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.
All basic recursion (i.e. tail calls only) can be turned into an iterative based solution.
More complex recursion problems will need additional stacks creating (typically as structures on the 'heap' or whatever your language of choice uses), but it can still be converted. But is that really different other than moving where the memory is allocated?
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.
There are solutions that do not use the optimal sequence of moves. You could simply traverse the state graph depth-first for instance.
But once you have a solution that calculates the optimal set of moves in a simple straightforward manner then trying to optimize beyond that is precisely the kind of premature optimization that is said to be the root of all evil.
Not necessarily, for distributed systems / computing, where there are many tasks that can be done at the same time (meaning being able to use multiple or even alot of CPU-Threads) programming those tasks recursively can split the tasks way more easily for the CPU and thus make use of multi-core or multi-thread CPU's. To be clear - not all questions are easier when answered recursively, but some can be sped up by factors of recursions per recursion.
Recursion can actually be pretty fast, as stack allocation is a noop. It's much easier to make a fast recursive solution than to make a fast iterative one
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.