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 BAA18938; Mon, 17 Jun 2002 01:20:07 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA18884 for ; Mon, 17 Jun 2002 01:20:06 +0200 (MET DST) Received: from mel-rto1.wanadoo.fr (smtp-out-1.wanadoo.fr [193.252.19.188]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g5GNGwP23586 for ; Mon, 17 Jun 2002 01:17:02 +0200 (MET DST) Received: from mel-rta10.wanadoo.fr (193.252.19.193) by mel-rto1.wanadoo.fr (6.5.007) id 3D0A0BEA000ECA3B for caml-list@inria.fr; Mon, 17 Jun 2002 01:16:57 +0200 Received: from debian (80.8.81.20) by mel-rta10.wanadoo.fr (6.5.007) id 3CFB2CF900857762 for caml-list@inria.fr; Mon, 17 Jun 2002 01:16:57 +0200 Received: from moi by debian with local (Exim 3.35 #1 (Debian)) id 17JjIn-0000Oj-00 for ; Mon, 17 Jun 2002 01:19:21 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] static variables in a function References: <20020614170830.28193.qmail@web10705.mail.yahoo.com> <20020615114201.B1425@max.home> <864rg5oxpo.fsf@laurelin.dementia.org> <20020615215124.C1425@max.home> <86y9dgo6y6.fsf@laurelin.dementia.org> <20020616021902.A22564@max.home> From: Remi VANICAT Date: Mon, 17 Jun 2002 01:19:21 +0200 In-Reply-To: <20020616021902.A22564@max.home> (Max Kirillov's message of "Sun, 16 Jun 2002 02:19:02 +0700") Message-ID: <87k7oyreva.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 Max Kirillov writes: > On Sat, Jun 15, 2002 at 12:14:09PM -0400, John Prevost wrote: > <...> >> If we change our focus, howeverm the technique becomes more >> interesting. Take a look at this, for example: > <...> >> let memoize f = > > Hmm... this isn't executed at read time because function has > a parameter... this code : 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 was correct : it create a new Hastable for each different function one want to memoize. Otherwise the hastable will be shared between different function, what we realy don't want. look at this for a proof of what I'm saying : # let print_once = memoize print_endline;; val print_once : string -> unit = # print_once "foo";; foo - : unit = () # print_once "bar";; bar - : unit = () # print_once "bar";; - : unit = () # the last time, the value isn't recompile (and then nothing is printed), so the memoization have worked. -- 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