From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA24256; Tue, 18 Jun 2002 16:05:11 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA24501 for ; Tue, 18 Jun 2002 16:05:10 +0200 (MET DST) Received: from mel-rto2.wanadoo.fr (smtp-out-2.wanadoo.fr [193.252.19.254]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g5IE59b26195 for ; Tue, 18 Jun 2002 16:05:09 +0200 (MET DST) Received: from mel-rta10.wanadoo.fr (193.252.19.193) by mel-rto2.wanadoo.fr (6.5.007) id 3D0DC46B000C1A43 for caml-list@inria.fr; Tue, 18 Jun 2002 16:05:09 +0200 Received: from debian (80.8.82.65) by mel-rta10.wanadoo.fr (6.5.007) id 3CFB2CF900960882 for caml-list@inria.fr; Tue, 18 Jun 2002 16:05:09 +0200 Received: from moi by debian with local (Exim 3.35 #1 (Debian)) id 17KJdy-0001Io-00 for ; Tue, 18 Jun 2002 16:07:38 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] Memoizing (was: static variables...) References: <87k7oyreva.dlv@wanadoo.fr> <200206171356.g5HDu5u19866@n05.sp.go.dlr.de> <20020618084006.GA8596@force.stwing.upenn.edu> <3D0F3099.1060809@htec.demon.co.uk> From: Remi VANICAT Date: Tue, 18 Jun 2002 16:07:38 +0200 In-Reply-To: <3D0F3099.1060809@htec.demon.co.uk> (Christopher Quinn's message of "Tue, 18 Jun 2002 14:07:37 +0100") Message-ID: <87znxs8ytx.dlv@wanadoo.fr> User-Agent: Gnus/5.090006 (Oort Gnus v0.06) Emacs/21.2 (i386-debian-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Christopher Quinn writes: [...] > 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! no, an exception can be caused by the evaluation of the function to memoize. then the result of evaluating the function (which is the Exn) will be stored. By the way, one shouldn't catch the Stack_overflow exception, as it is not really the result of the evaluation... > > 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 this memoize function have several problem : - it is not fully polymorphic (you have '_a type) - you cannot apply this function to two different function : let even' odd' x = if x = 0 then true else (odd' (x - 1)) let odd' even' x = if x = 0 then false else (even' (x - 1)) let rec even x = even' odd x and odd x = odd' even x this work, but let rec meven x = even' (memoize modd) x and modd x = odd' (memoize meven) x won't (as they will both use the same hastable) the only way I see to resolve all those problem is to make: let new_memoize () = let stow = Hashtbl.create 20 fun f x -> try Hashtbl.find stow x with Not_found -> let v = f x in Hashtbl.add stow x v; v each call to new_memoize () will make a new memoize function that could be apply to one function. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- 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