rememb.tex 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. \section{REMEMBER Statement}\ttindex{REMEMBER}
  2. Setting the remember option for an algebraic procedure by
  3. \begin{verbatim}
  4. REMEMBER (PROCNAME:procedure);
  5. \end{verbatim}
  6. saves all intermediate results of such procedure evaluations, including
  7. recursive calls. Subsequent calls to the procedure can then be determined
  8. from the saved results, and thus the number of evaluations (or the
  9. complexity) can be reduced. This mode of evalation costs extra memory, of
  10. course. In addition, the procedure must be free of side--effects.
  11. The following examples show the effect of the remember statement
  12. on two well--known examples.
  13. \begin{samepage}
  14. \begin{verbatim}
  15. procedure H(n); % Hofstadter's function
  16. if numberp n then
  17. << cnn := cnn +1; % counts the calls
  18. if n < 3 then 1 else H(n-H(n-1))+H(n-H(n-2))>>;
  19. remember h;
  20. > << cnn := 0; H(100); cnn>>;
  21. 100
  22. % H has been called 100 times only.
  23. procedure A(m,n); % Ackermann function
  24. if m=0 then n+1 else
  25. if n=0 then A(m-1,1) else
  26. A(m-1,A(m,n-1));
  27. remember a;
  28. A(3,3);
  29. \end{verbatim}
  30. \end{samepage}