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: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] Memoizing (was: static variables...)
William Lovas wrote:

>
>    let recfib recfib = function
>      | 0 | 1 -> 1
>      | n -> recfib (n-1) + recfib (n-2);;
>

>
>Does anyone have any thoughts on this?  Am i missing something obvious?
>

let fib = ref (function | _ -> 1);;
fib := function  | 0 | 1 -> 1 | n-> !fib (n-1) + !fib (n-2) ;;
let memo h f x = (if not (Hashtbl.mem h x) then Hashtbl.add h  x (f x); 
Hashtbl.find h x);;
let h = Hashtbl.create 97;;
fib := memo h !fib;;



-- 
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850




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