Given code that looks something like this (pseudocode):
f(args)
result = simple_case
if (base_case(args))
return result
new_args = args
for each x in args list
remove x from new_args
if (need_to_update_result_based_on_x(result,x))
result = f(new_args)
return result
I already know how to remove tail-recursion from a function, but I am unsure if there are straightforward techniques for removing recursion that itself is inside of a loop, as above.
In particular, I am especially wondering if it is it possible to rewrite it so that it is purely iterative? By which I mean that it does not even need to effectively emulate the recursive design by simply implementing it's own stack? If not, what would generally be the most economical (in terms of storage and time) way to rewrite such a function?