Common Lisp Package: DEFMEMO

README:

Defmemo

Defmemo is a generic defun form, performing memoization over calls to such defined function. It supports arbitrary lambda lists and multiple return values. (optimize speed)-friendly, otherwise it might have been implemented as in Peter Norvig's PAIP. Preserves arguments and documentation. Memoizing hash table is stored under property :memo of defmemo'ed function.

Usage

Three functions are exported: defmemo (like defun), get-memo and clear-memo (defmemo'ed symbol). Clear-memo is not needed on implementations supporting weak hash tables (via trivial-garbage).

`lisp (defmemo fib (n) (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))

(fib 100) ; => 573147844013817084101

(get-memo 'fib) ; => #<hash-table :TEST equal :COUNT 101 :WEAKNESS :key>

(hash-table-count (clear-memo 'fib)) ; => 0 `

FUNCTION

Public

CLEAR-MEMO (SYMBOL)

Reset memoizing hash table.

GET-MEMO (SYMBOL)

Get memoizing hash table.

SETFGET-MEMO (VALUE SYMBOL)

Replace memoizing hash of symbol with value.

Private

FLAT-ARGLIST (ARGS)

Convert ordinary lambda list into funcallable list.

MACRO

Public

DEFMEMO (NAME ARGS &BODY BODY)

Construct defun with body wrapped into memoizing hash table. Put the latter under :memo property of name.