Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Benedikt Rosenau <Benedikt.Rosenau@d...>
Subject: [Caml-list] Memoizing (was: static variables...)
Remi Vanicat wrote:

[...]

> let memoize f =
>   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

I tried to memoize the Ackermann function

# let rec ack m n =
    if m = 0 then n + 1
    else if n = 0 then ack (m - 1) 1
    else ack (m - 1) (ack m (n - 1))
val ack : int -> int -> int = <fun>
# let my_ack = memoize ack;;
val my_ack : int -> int -> int = <fun>


The type checker deals with the 'a being an (int -> int).
However, I noticed no speedup. So, I transformed the
Ackermann to the tupled version, ie "let ackermann (m, n)...",
but to no avail.

What is the mistake?

Regards,
   Benedikt
-------------------
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