Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] [q] on implementing of "memoization"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: SooHyoung Oh <shoh@d...>
Subject: [Caml-list] [q] on implementing of "memoization"

In testing "memoization" of fib function, it is OK on
    let rec fib = ... and memo_fib n = memoize fib n;;
but it makes the ERROR on
    let rec fib = ... and memo_fib = memoize fib;;

The error message is
      This kind of expression is not allowed as right-hand side of `let rec'

Why does this error occur?
I can't find good description on ocaml manual.

One more thing:
Do you have any idea about ** generic ** and good implementation of
memoization?

The source is
----------------------------------
let lookup key table = List.assoc key !table;;
let insert key value table = table := (key, value) :: !table;;

let table = ref [];;
let memoize f n =
    try
        lookup n table
    with
    | Not_found ->
        let result = f n in
        insert n result table;
        result
;;

let rec fib = function
    | 0 -> 0
    | 1 -> 1
    | n -> memo_fib (n-1) + memo_fib (n-2)
and memo_fib n = memoize fib n
;;
----------------------

Thanks a lot!

---
SooHyoung Oh

-------------------
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