问题
What is the best way to check that a function has been tail call optimized in ATS? (So far I have been running "top" to see if memory usage is constant)
As a follow up: Say you have a complex tail-recursive function that the compiler has failed to TCO, is there a way to re-write it in a more compiler friendly way? Or in such a way to force the compiler to attempt TCO?
回答1:
There is a peculiar way to do it in ATS2.
Say you have
fnx foo(...) = bar(...)
and bar(...) = ...bar...
If the body of bar contains a non-tail-recursive call to bar, then the C compiler is to complain with an error message.
Things become a lot more challenging when (linear) streams are involved. A seemingly non-tail-recursive function can run without concern of stack overflow because it essentially saves its stack on heap (and then frees it): This is a place where ATS really shines!
来源:https://stackoverflow.com/questions/49222337/compiler-friendly-tail-recursion-tail-recursion-checking-in-ats