Next: , Up: Higher Ordered Fun


23.9.1 Memoization

To make use of the memoization facilities, evaluate the form:

     (use-modules (ice-9 poe))

This provides two “purity of essence” [insert actual Vonnegut allusion here, someday, or not] procedures for memoizing pure functions.

A pure function (of some sort) is characterized by two equality relations: one on argument lists and one on return values. A pure function is one that when applied to equal arguments lists yields equal results.

If the equality relationship on return values can be eq?, it may make sense to cache values returned by the function. Choosing the right equality relation on arguments is tricky.

The simplest case of pure functions are those in which results are only certainly eq? if all of the arguments are.

— Scheme Procedure: pure-funcq base

Return a procedure pf that wraps procedure base, associating the arg list of each call to pf to its return value in a globally shared (but bounded nonetheless) table.

— Scheme Procedure: perfect-funcq size base

Return a procedure pf that wraps procedure base, associating the arg list of each call to pf to its return value in a private table of roughly size elements. Thus: “A pure funq may sometimes forget its past but a perfect funcq never does.”