Final answer:
To create a factorial program in Lisp that uses memoization, a global array is defined to store computed factorials. The factorial function checks the array for the result before calculating a new one. If a new factorial is computed, it's stored in the array for future use.
Step-by-step explanation:
The task is to write a program in Lisp that computes the factorial of a given number and stores the computed factorials in a global array. The approach I would suggest involves using a technique called memoization, which is an optimization technique used to increase the performance of programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
To implement memoization in Lisp, we would define a global array to hold computed factorials. Then, we write the factorial function that checks this array before performing any calculations. If the result is already in the array, it returns that result; otherwise, it computes the factorial, stores the result in the array, and then returns the computed factorial.
Here's an outline of how the code would look:
(defvar *memo-factorials* (make-array 10 :initial-element NIL))
(defun memo-factorial (n)
(or (aref *memo-factorials* n)
(setf (aref *memo-factorials* n)
(if (<= n 1)
1
(* n (memo-factorial (1- n)))))))
In the above code snippet, we define a global variable *memo-factorials* as an array to store the computed factorials. The memo-factorial function checks the array for a stored value and uses it if available. Otherwise, it computes the value, stores it, and returns it. This array is dynamically sized, but you can adjust the size to suit your needs.