On Sat, Jun 15, 2002 at 12:14:09PM -0400, John Prevost wrote: <...> > If we change our focus, howeverm the technique becomes more > interesting. Take a look at this, for example: <...> > let memoize f = Hmm... this isn't executed at read time because function has a parameter... > let stow = Hashtbl.create 20 in > fun x -> begin > if not (Hashtbl.mem stow x) then begin > try (let v = f x in Hashtbl.replace stow x (Val v)) > with e -> Hashtbl.replace stow x (Exn e) > end; > match Hashtbl.find stow x with > | Val x -> x > | Exn e -> raise e > end <...> > let rec fib_r2 x = > memoize (fib' fib_r2) x The function memoize (and even "memoize (fib' fib_r2)" with a variable first parameter) as called at every recursion step and therefore no caching will be performed. (I've tested it). The working way is: let fib_r3 = memoize fib_r1 which is, btw, very similar to let fib_r3 = new memoize fib_r1 > In this case, the whole point of the function is that it produces a > function identical in every way to the original function, except that > it memoizes its work so as not to repeat. There's no convenient > "object" to stuff the value into, because the function *is* our > object. The only way we can store the data correctly is to have the > information somehow attached to the function we're creating. Well, OO-biased programmer would say it's a good reson to _make_ an object with only one exported method. Nearly any programmer would say that we should make an object to provide additional control, say, to clear the cache. I've played with it a bit, and discover that there are many ways to do the job. Three files attached contains three ways of doing the same things via function returned from initialization func (accPart.ml), commonly known OO-patterns (accClass.ml) and modules system (accMod.ml). Tha task is to remember a function and initial value, then sending operands for successive applications of function. To allow several "methods" to deal with data object we must return several function, not just one. Moreover, we cannot use inheritance, as we can with classes. accMod.ml discovers generally the same features as accClass.ml, but contains a bit more verbose writing. Maybe, modules is not a good way to contain persistent mutable value. Max. PS: maybe in version2 one could use record with labelled things, to make it a bit nicer.