Question: In this task, we define a function called fib mem, which is the memoized version of the Fibonacci function, which takes an integer n as
In this task, we define a function called fib mem, which is the memoized version of the Fibonacci function, which takes an integer n as input and returns the n-th number in the Fibonacci sequence.
Define the Fibonacci function fib as usual.
Define the bind and lookup functions for association lists, as we discussed in class. Recall that an association list in Scheme is just a list of pairs and each pair contains a key and a value. (bind k v al) adds a new entry (k,v) to the beginning of association list al. (lookup k al) returns the value for key k in al if there is an entry for k and returns #f otherwise.
Define a global variable al for the association list used in fib mem. (define al ())
Finally, define fib mem. When given n, it checks whether there is an entry for n in al. If there is, it returns the value in the entry; if not, it invokes (fib n), adds the entry (n, (fib n)) in the association list, and returns (fib n).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
