15.6 REMEMBER Statement

Setting the remember option for an algebraic procedure by

     REMEMBER (PROCNAME:procedure);

saves all intermediate results of such procedure evaluations, including recursive calls. Subsequent calls to the procedure can then be determined from the saved results, and thus the number of evaluations (or the complexity) can be reduced. This mode of evalation costs extra memory, of course. In addition, the procedure must be free of side–effects.

The following examples show the effect of the remember statement on two well–known examples.

procedure H(n);      % Hofstadter’s function  
 if numberp n then  
 << cnn := cnn +1;   % counts the calls  
 if n < 3 then 1 else H(n-H(n-1))+H(n-H(n-2))>>;  
remember h;  
<< cnn := 0; H(100); cnn>>;  
% H has been called 100 times only.  
procedure A(m,n);    % Ackermann function  
 if m=0 then n+1 else  
  if n=0 then A(m-1,1) else  
remember a;