3.8 Designing recursive functions around Python’s stack limits
Some functions can be defined clearly and succinctly using a recursive formula. There are two common examples of this.
The factorial function has the following recursive definition:
The recursive rule for computing a Fibonacci number, Fn, has the following definition:
Each of these involves a case that has a simple defined value and a case that involves computing the function’s value, based on other values of the same function.
The problem we have is that Python imposes an upper limit for these kinds of recursive function evaluations. While Python’s integers can easily compute the value of 1000!, the stack limit prevents us from computing this casually.
Pragmatically, the filesystem is an example of a recursive data structure. Each directory contains subdirectories. Recursive function definitions can be used...