Hacker News new | ask | show | jobs
by balfirevic 1908 days ago
> Been doing this for 10 years and have never come across a data problem that only recursion could solve

That makes sense - there are no such problems (which is obvious if you consider the fact that Turing machines don't have recursion - nor functions, for that matter).

> I asked the interviewer to give me one scenario where a recursive solution would be the most efficient one

That also makes sense, as recursion usually isn't a technique that will bring an efficiency edge over non-recursive technique. It might often bring maintainability and understandability edge, though.

Solving the problem using recursion highlights the fact that it has, well, a recursive solution - which means that the solution can be expressed as a set of operations on top of solution to a smaller instance of the original problem. This can be an important property to notice about a problem, whether or not you're going to solve it with the function that calls itself.

Additionally, some problems have a solution that can be nicely expressed as a set of mutually recursive functions (instead of just a single recursive function). These are harder to express non-recursively. Many parsers are written like this, for example the C# parser: https://news.ycombinator.com/item?id=13915150

1 comments

It would be more difficult to read and maintain any solution to many problems where you are operating on nested objects, especially if the nesting is more than a few levels deep or even arbitrarily deep, without using recursion. Also generating such objects, such as with nested parentheses, is easier as well.