Add notes for 3.27

This commit is contained in:
Oliver Payne 2022-05-17 22:07:04 +01:00
parent 69afb16beb
commit 0a4066baf2
1 changed files with 15 additions and 0 deletions

View File

@ -116,3 +116,18 @@ For z as in the example,
(sqrt (+ (square 3) (square 4)))
5
3.27
For i = 0 or 1, (memo-fib i) is O(1). For i > 1, it is n additions. This is because
for each i, (memo-fib i) is only calculated once. This calculation is O(n) and each subsequent
lookup is O(1). So, even though (memo-fib i) is called multiple times for each i, only i additions are
done for each.
This scheme will not work if we use (define memo-fib (memoize fib)). The fib procedure is
defined recursively in terms of itself, and each of the these recursive calls will be evaluated in
an environment whose enclosing environment is the global environment. This explicitly does not include
the table of pre calculated results, so we will not get the benefit of this. Defining memo-fib recursively
in terms of itself means that the recursive calls are evaluated in an environment whose enclosing
environment is one where memoize has been evaluated, and thus it contains the table. This is critical
to make the memoization work as required.