I have seen these questions:
- Can and do compilers convert recursive logic to equivalent non-recursive logic?
- Way to go from recursion to iteration
- Do any programming language compilers automatically convert recursion to iteration?
These all mention tail-call optimization. I am wondering though if, given a function such as what follows, it is possible to automatically "flatten" it from recursive to iterative, using something other than tail-call optimization (which I don't think applies in this case, and I wouldn't want to place that restriction otherwise).
a() {
while x < 3 {
b()
}
}
b() {
c()
}
c() {
d()
}
d() {
a()
}
Notice d
calls a
and starts the recursion over, but assume that it won't recurse forever, just a few levels deep (this is just pseudocode). This is different than the fibonacci example which just has 1 function recursively calling itself. I'm wondering if any research has been done on the topic of automatically converting complex recursive functions like this into iterative ones.