defmacro defun-mem name params body body let name-vals gensym value ge

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
(defmacro defun-mem (name params &body body)
(let ((*name-vals* (gensym))
(value (gensym))
(exist (gensym)))
`(progn
(defvar ,*name-vals* (make-hash-table))
(defun ,name ,params
(multiple-value-bind
(,value ,exist)
(gethash (list ,@params) ,*name-vals*)
(if ,exist
,value
(setf
(gethash (list ,@params) ,*name-vals*)
(progn ,@body))))))))
(format t "~a~%" (macroexpand-1
'(defun-mem ack-mem (m n)
(if (= m 0)
(+ n 1)
(if (= n 0)
(ack-mem (- m 1) 1)
(ack-mem (- m 1) (ack-mem m (- n 1))))))))