Recursive solutions benefit from recursive solutions unless the solution is trivial.
It depends on whether the limited call stack capacity will be an issue for the particular problem you’re solving.
I’m presently working on a problem which uses traversal of TypeScript file syntax trees.
I can reasonably assume that we will never get a file with a deep enough syntax tree which would cause a stack overflow.
A manually managed stack might seem safer, but as pointed out by this article the code would be more complicated and, in my case, for no good reason.
This page is a good example of when Typescript is both unnecessary and off-putting, based on the content's purpose.
The author is trying to demonstrate a problem and the proposed solution, using example code that the reader then needs to read, formulate a visual "structure" in their mind, and then apply that structure to the subsequent code.
Typescript is not in any way invalid here, and as a tool when programming something that will run in the real-world, can be invaluable.
However, when writing anything you want someone to deeply comprehend, you want to use the least amount of text needed to get your point across. Adding types don't serve any purpose here. The types used are generics, which tell you nothing specific, hence the name, and the names given to the functions and object properties are enough to convey what this code is doing.
PL-specific problem, but: the Rust borrow checker tends to give you a lot of trouble when writing iterative algorithms on nonlinear data structures, because it has trouble reasoning about partial borrows.
The upcoming Polonius borrow checker is supposed to solve this, but it's still in alpha...
This has always felt like the kind of thing we could be building compilers to solve. Recursive -> explicit stack is a very mechanical conversion, why can't we write a compiler transform pass that does that rewrite any time it detects a high potential for stack overflow?
> It seems to be common knowledge that any recursive function can be transformed into an iterative function.
Huh. Where i work, the main problem is that everyone is hell-bent on transforming every iterative function into a recursive function. If i had a pound for every recursive function called "loop" in the codebase, i could retire.
[dead]
> This solution looks extremely similar to the previous one, which is a good thing. Our requirements have experienced a small change (reversing the traversal order) and our solution has responded with a small modification.
Now do breadth-first traversal. With the iterative approach, you just replace the stack with a queue. With the recursive approach, you have to make radical changes. You can make either approach look natural and elegant if you pick the right example.