Re:Curious C++ Lambda Examples: Recursion, constexpr, Contai...
by hot_gril on Sunday August 14, 2022 @05:07AM
(#32457716)
Recursion as a concept is useful. In practice, you can sometimes achieve better performance (or avoid exceeding some call stack limit) by doing it "iteratively" in a way that's still conceptually equivalent to recursion, i.e. with a stack you manually manage. I've never had a function call itself in C++, but I've done conceptually recursive things a lot in it.
[ Parent
| on HN ]
Re:Curious C++ Lambda Examples: Recursion, constexpr, Contai...
by fooker on Sunday August 14, 2022 @05:51AM
(#32457935)
Manually managing a stack is significantly more expensive compared to utilizing the call stack for recursion. Modern hardware and calling conventions are designed to make call stacks fast.
You should measure this!
Re:Curious C++ Lambda Examples: Recursion, constexpr, Contai...
by hot_gril on Sunday August 14, 2022 @06:05AM
(#32458001)
For my job involving C++, it doesn't matter which is faster. Just doing whatever is more readable, and since I'm usually doing a DFS/BFS func, it's nice being able to easily toggle that by popping from front vs back. When I'm not, I'd rather have a readable stack trace if something goes wrong and not have to deal with the oddities of recursive lambdas.
In Python, the issue is the easily reachable max recursion depth. Tail recursion isn't optimized.
In Erlang, you use recursion even just to iterate through a list. It's fine, the language is made for that.