News for hackers. Stuff that matters.
Re:Thinking About Recursion by skybrian on Sunday October 11, 2020 @06:09PM (#24749515)
Whether it's simpler depends on the problem. There was a programming interview question I used to use where you'd typically build a tree and print it out. I would warn people against trying to build the tree recursively because it never went well. (Printing it out was fine.)

I think part of it was that to use recursion properly in this case, you'd have to return multiple values, and that complicated things, particularly if they were using a language like Java where it's not built-in.

Re:Thinking About Recursion by Jtsummers on Sunday October 11, 2020 @06:18PM (#24749586)
Why would building a tree require returning multiple values? Was this something specific to your problem statement? Typical tree insert algorithms are usually easily expressed recursively (though since they can be tail recursive, iterative versions are usually also easy to write).
Re:Thinking About Recursion by skybrian on Sunday October 11, 2020 @06:39PM (#24749735)
It was effectively a parsing problem. When building a tree recursively, it's natural to return the finished subtree, but more information was needed to keep track of what gets parsed next. You would need to return both the result and the start of the unparsed input.

In imperative languages, parsers are often written using the fields of an object or struct to hold the parser state. But if you're just writing a single function then it becomes more natural to keep the parser state in local variables, and recursion means you don't have easy access to it.

Re:Thinking About Recursion by Jtsummers on Sunday October 11, 2020 @07:24PM (#24749990)
That makes sense, thanks.
Re:Thinking About Recursion by xupybd on Sunday October 11, 2020 @08:29PM (#24750357)
>>Whether it's simpler depends on the problem.

I couldn't agree more. One down side of recursion can be that if you need to refactor you might find recursion is no longer the better solution. Unwinding that can be a pain.