3 ms·
Why would building a tree require returning multiple values? Was this something specific to your problem statement? Typical tree insert algorithms are usually e
by Jtsummers 6y ago
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).
- skybrian 6y agoIt 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.
- Jtsummers 6y agoThat makes sense, thanks.