Browse thread
[Caml-list] Memoizing (was: static variables...)
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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;
v
- chris
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners