[Ecls-list] memoize compiled function

Alessandro Serra gas.hale at gmail.com
Mon Mar 29 08:36:59 UTC 2010


Hi all,
I was playing with "memoize" when I found out that I cannot do it with
recursive compiled functions. A recursive function is usually compiled
as:

>   static cl_object L1f(cl_object V1)
>        ...
>	T1= L1f(T0)
>        ...
The redefinition of "f":
> (setf (symbol-function 'f) ...)
 has not effect on the recursive calls of f.

I'm not an expert of common lisp standard. I would like to know if the
ecl's behaviour is correct.
Thanks,
Alessandro


>(defun memo (fn)
>  (let ((table (make-hash-table :test #'eql)))
>   (lambda (arg)
>      (multiple-value-bind (val valp)
>	  (gethash arg table)
>	(if valp
>	    val
>	    (setf (gethash arg table) (funcall fn arg)))))))

> (defun memoize (fn-name)
>  "Replace fn-name's global definition with a memoized version."
> (setf (symbol-function fn-name) (memo (symbol-function fn-name))))


-- 
The basic tool for the manipulation of reality is the manipulation of
words. If you can control the meaning of words, you can control the
people who must use the words.
                   How To Build A Universe That Doesn't Fall Apart Two
Days Later
                   Philip K. Dick




More information about the ecl-devel mailing list