Best way to make mutually recursive functions iterative?

What is the best approach to make a mutually recursive function iterative?
Can you give us some pseudo-code?
Do you mean "mutually", as in:
1
2
3
4
5
6
7
T f( args ) {
  g( something );
}

U g( args ) {
  f( something );
}


It does depend on the type of recursion.
Yes. I need to make it iterative. Would using goto be an approach? What is the best approach? I can't make them tail recursive (which is essentially iterative on most compilers) so that is not an option.
Can you write some your own pseudo-code to express your idea so that we can translate it to C++ language the way you want?
Here is the most stupid and trivial example I can think of:
1
2
3
4
5
6
7
8
9
bool is_even(unsigned x) {
  if (x == 0) return true;
  else return is_odd(x - 1);
}

bool is_odd(unsigned x) { 
  if (x == 0) return false;
  else return is_even(x - 1);
}


Here's one way to make it iterative.
1
2
3
4
5
bool is_even(unsigned x) {
  bool result = false;
  while (x-- != 0) result = !result; 
  return result;
}


You need to show some code. The form of the iterative solution depends on the nature of the problem you're solving.
Last edited on
Topic archived. No new replies allowed.