(defun memoize (fn)
(let ((table (make-hash-table)))
(lambda (x)
(or (gethash x table)
(setf (gethash x table) (funcall fn x))))))
;; Используется так:
(time
(let ((fn (memoize (lambda (x) (sleep x) x))))
(print (funcall fn 3))
(print (funcall fn 3))))
;; Или так:
(defun yoba (x) (sleep x) x)
(setf (fdefinition 'yoba) (memoize #'yoba))
(time (progn (print (yoba 1))
(print (yoba 1))))
KGRlZnVuIG1lbW9pemUgKGZuKQogIChsZXQgKCh0YWJsZSAobWFrZS1oYXNoLXRhYmxlKSkpCiAgICAobGFtYmRhICh4KQogICAgICAob3IgKGdldGhhc2ggeCB0YWJsZSkKICAgICAgICAgIChzZXRmIChnZXRoYXNoIHggdGFibGUpIChmdW5jYWxsIGZuIHgpKSkpKSkKCjs7INCY0YHQv9C+0LvRjNC30YPQtdGC0YHRjyDRgtCw0Lo6Cih0aW1lCiAgKGxldCAoKGZuIChtZW1vaXplIChsYW1iZGEgKHgpIChzbGVlcCB4KSB4KSkpKQogICAgKHByaW50IChmdW5jYWxsIGZuIDMpKQogICAgKHByaW50IChmdW5jYWxsIGZuIDMpKSkpCgo7OyDQmNC70Lgg0YLQsNC6OgooZGVmdW4geW9iYSAoeCkgKHNsZWVwIHgpIHgpCihzZXRmIChmZGVmaW5pdGlvbiAneW9iYSkgKG1lbW9pemUgIyd5b2JhKSkKCih0aW1lIChwcm9nbiAocHJpbnQgKHlvYmEgMSkpIAogICAgICAgICAgICAgKHByaW50ICh5b2JhIDEpKSkp