3 ms·
What about just make a new def-macro for python? A very simple common lisp-implementation would be something like this: (defmacro defmemoize (fn val &body bo
by JeanPierre 16y ago
What about just make a new def-macro for python? A very simple common lisp-implementation would be something like this:
(defmacro defmemoize (fn val &body body)
(with-gensyms (lst hash-code)
`(let ((,lst (make-hash-table :test #'equal)))
(defun ,fn ,val
(let ((,hash-code (list ,@val)))
(or
(gethash ,hash-code ,lst)
(setf (gethash ,hash-code ,lst)
,@body)))))))
In which the standard fibonacci-example would be
(defmemoize fib (n)
(if (<= n 2) ; first two = 1
1
(+ (fib (1- n))
(fib (- n 2)))))
Obviously, it would be something more advanced than this, but it shouldn't have to be much more advanced.
- joubert 16y agoI like the atom construct in Clojure - see a functional implementation of memoization, instead of one based on macrology: http://clojure.org/atoms http://clojure.org/atoms