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 QAA03500; Sat, 15 Jun 2002 16:55:02 +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 QAA03414 for ; Sat, 15 Jun 2002 16:55:01 +0200 (MET DST) Received: from ntlg.sibnet.ru (dns.sibnet.ru [217.70.96.34]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g5FEswP27675 for ; Sat, 15 Jun 2002 16:54:59 +0200 (MET DST) Received: from sibnet.ru (tlg5-ppp71.sibnet.ru [217.70.98.202]) by ntlg.sibnet.ru (8.9.3+Sun/8.9.3) with ESMTP id SAA10537 for ; Sat, 15 Jun 2002 18:54:47 +0400 (MSD) Received: by sibnet.ru id m17JEtg-001En5C; Sat, 15 Jun 2002 21:51:24 +0700 (NOVST) Date: Sat, 15 Jun 2002 21:51:24 +0700 From: Max Kirillov To: caml-list@inria.fr Subject: Re: [Caml-list] static variables in a function Message-ID: <20020615215124.C1425@max.home> Mail-Followup-To: caml-list@inria.fr References: <20020614170830.28193.qmail@web10705.mail.yahoo.com> <20020615114201.B1425@max.home> <864rg5oxpo.fsf@laurelin.dementia.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <864rg5oxpo.fsf@laurelin.dementia.org>; from j.prevost@cs.cmu.edu on Sat, Jun 15, 2002 at 02:36:03AM -0400 X-Sender: Max Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Oh, thank you for clarification. I'm very new to functional programming, and didn't know yet about tricks like this. As I understand, the point was to call 'ref' at initialization stage. I thought about using 'lazy' for that, but still placed it inside a full function definition, so it didn't help. This makes a possibility to write very funny functions. For example: # let acc v = let vR = ref v in fun f -> let newV = f !vR in vR := newV; newV ;; val acc : 'a -> ('a -> 'a) -> 'a = # let f1 = acc 12;; val f1 : (int -> int) -> int = # f1 ((+) 1);; - : int = 13 # f1 ((+) 1);; - : int = 14 # f1 ((+) 1);; - : int = 15 # f1 ((+) 1);; - : int = 16 # f1 ((+) 1);; - : int = 17 # let f2 = acc "hello";; val f2 : (string -> string) -> string = # f2 ((^) " a");; - : string = " ahello" # f2 ((^) " b");; - : string = " b ahello" # f2 ((^) " c");; - : string = " c b ahello" I'm not sure if this style of coding better than placing the mutable values to be hidden inside modules or classes. Even when possible (in other languages), I prefer to use latter way to do. Well it's a matter of taste. Max. On Sat, Jun 15, 2002 at 02:36:03AM -0400, John Prevost wrote: >>>>>> "mk" == Max Kirillov writes: > >mk> Hello. The code you write will generate a new (empty) ref at >mk> every call. > > Actually, it won't. Take a look at the code that was sent by Yutaka OIWA: > > let get_chunk = > let chunks_list = ref [] in > fun () -> > ... > > The ref is bound outside of the function definition, so it's created > once and kept in the closure, not allocated in each call. <...> > Your version also works, but creates a module-level binding for the > value as well, which isn't desirable for something like this, where > you want the value to be local to the function definition. > > John. ------------------- 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