I think you need to show us some code. My guess is that you are using some kind of global variable, which means that you will need to rewrite your code to save the old value, set the new value, recurse, and then restore the old value again. The following Perl code works fine with recursing and automatically resets the old value:
use strict;
print ackermann(3,3);
sub ackermann {
my ($left,$right) = @_;
print "$left,$right\n";
if ($left == 0) {
return $right+1
} elsif ($right == 0) {
return ackermann($left-1, 1)
} else {
return ackermann($left-1, ackermann($left, $right-1))
};
};
The trick is, that this code does not use respectively change any global variables, and thus has no problems invoking itself. |