I wonder if one of the Elixir tools can / should / already does have an option to report instances of non-tail-call recursion, in order to help developers notice this infelicity. AFAIK, the compiler already has to check for this; what about adding an option or perhaps an attribute to ask for reporting on this?
Body recursion is 100% appropriate in lots of circumstances. There are some technical issues with what you’re asking too but before we dive into that, can you elaborate more about the issue you’re trying to prevent?
After decades of dabbling in recursion, I’m only barely competent at writing code in that style. So, I generally make do with pipelines of calls to filter, map, reduce, etc. Even when I get some recursive code running, I may not be using a tail call.
However, if some program would tell me “this routine isn’t using a tail call”, I might be able to look over the code and fiddle with it until the program is happy. I could then decide whether I think the change improved things, from the standpoint of performance, readability, etc.
Tail recursion is not always better than body recursion; and the difference is usually not that big anyway. For example in this case:
My advice are:
- Use idiomatic ways and standard libraries for common tasks
- Write the code in the way that is the clearest to you
- If in doubt, benchmark/profile
This advice seems very reasonable to me. Basically, the only time tail recursion is likely to make a substantial difference is when it’s being used in a processing-intensive “inner loop”.
Even then (as always), profiling is strongly advisable before attempting any code optimization. I’m also very much in agreement with trying to keep the code clear and idiomatic. So, I guess the take-away is “nothing to see here; move along …”.