I tried to build a function as follows:
a(0,m) = m+1
a(n+1,0) = a(n,1)
a(n+1,m+1) = a(n,a(n+1,m))
First try was :
def a(n,m):
if n == 0:
return m + 1
elif m == 1:
return a(n ,0)
else:
return a(n - 1, a(n, m - 1))
and I got
RuntimeError: maximum recursion depth exceeded
So the second try was this and I worked.
def a(n,m):
if n == 0:
return m + 1
elif m == 0:
return a(n-1 , 1)
else:
return a(n - 1, a(n, m - 1))
So the question is I don't fully understand which difference in processing two functions why the first one got max. recursion depth exceeded error and second not ?
a(1, 0)it should calla(0, 1). That isa(0+1, 0) = a(0, 1) = 1 + 1. We can see this should be caught by the first base case. But instead it goes into the recursive case -- which is the cause of your infinite recursion. - Dunes