[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: general lazy list facility for Emacs Lisp?

From: Tassilo Horn
Subject: Re: general lazy list facility for Emacs Lisp?
Date: Wed, 23 Mar 2011 20:10:27 +0100
User-agent: Gnus/5.110016 (No Gnus v0.16) Emacs/24.0.50 (gnu/linux)

Tassilo Horn <address@hidden> writes:

> I think for "real" memoization, you need clojures.  However, I think
> you can simulate memoization with a macro like that (tested only very
> briefly).
> (defmacro memoize (name fun)
>   (let ((map-name (gensym "memo-map"))
>       (args-name (gensym))
>       (val-name (gensym)))
>     `(progn
>        (defvar ,map-name (make-hash-table :test 'equal))
>        (defun ,name (&rest ,args-name)
>        (let ((,val-name (gethash ,args-name ,map-name)))
>          (if ,val-name
>              ,val-name
>            (puthash ,args-name (apply (quote ,fun) ,args-name)
>                     ,map-name)))))))

Just a short remark on what I've had in mind with "real" memoization: If
elisp had closures (not clojures ;-)), then one could write memoize as
function, just like the variant in the clojure source code below:

--8<---------------cut here---------------start------------->8---
(defn memoize
  (let [mem (atom {})]
    (fn [& args]
      (if-let [e (find @mem args)]
        (val e)
        (let [ret (apply f args)]
          (swap! mem assoc args ret)
--8<---------------cut here---------------end--------------->8---


reply via email to

[Prev in Thread] Current Thread [Next in Thread]