Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Memoizing (was: static variables...)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-06-18 (13:07)
From: Christopher Quinn <cq@h...>
Subject: Re: [Caml-list] Memoizing (was: static variables...)
William Lovas wrote:
> Does anyone have any thoughts on this?  Am i missing something obvious?

One problem is the creation of the memoized function.
it has to be:

let rec mack v = memoize (ack mack) v

leaving out the v parameter is not possible and hence invocation of 
memoize is per invocation of mack, hence the hash is recreated empty 
each time.
Putting the hash into the top level solves this but then reclamation 
would need to be dealt with to avoid leakage.

Another point is that ack must be tuple-ized otherwise the memoization 
hash is a map int -> (int -> int),
which means actual results are not being stored.

That said, I do not understand the memoization function itself. Perhaps 
John Prevost could comment on the use of Val|Exn - in particular I 
cannot see how an initial Exn can be stored in the first place as an 
exception can only be caused by the presence of an Exn!

What is the advantage over specifying it this way? :

let stow = Hashtbl.create 20
let memoize f =
   fun x -> try
     Hashtbl.find stow x
   with Not_found ->
     let v = f x in
     Hashtbl.add stow x v;

- chris

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: