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 NAA09444; Mon, 11 Jun 2001 13:26:16 +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 NAA09375 for ; Mon, 11 Jun 2001 13:26:15 +0200 (MET DST) Received: from miss.wu-wien.ac.at (miss.wu-wien.ac.at [137.208.107.17]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f5BBQEv12650; Mon, 11 Jun 2001 13:26:14 +0200 (MET DST) Received: (from mottl@localhost) by miss.wu-wien.ac.at (8.9.0/8.9.0) id NAA23029; Mon, 11 Jun 2001 13:26:08 +0200 (MET DST) Date: Mon, 11 Jun 2001 13:26:08 +0200 From: Markus Mottl To: Francois Thomasset Cc: caml-list@inria.fr Subject: Re: [Caml-list] where is my count: newbie's question Message-ID: <20010611132608.B8432@miss.wu-wien.ac.at> References: <200106111021.f5BAL0L08064@ionie.inria.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <200106111021.f5BAL0L08064@ionie.inria.fr>; from Francois.Thomasset@inria.fr on Mon, Jun 11, 2001 at 12:21:00 +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Mon, 11 Jun 2001, Francois Thomasset wrote: > # let fnx(x) = let count = ref 0 in > let f() = > Printf.printf "x = %d\n" x; > Printf.printf "count = %d\n" !count; > count := !count + 1; !count > in f;; > val fnx : int -> unit -> int = > I would have expected that count remembers its previous value, which would be > incremented at each call, but this is not true : !count is always 0. > Of course if I remove the int argument I retrieve the behavior I expected: The reason is that "ref 0" is a function, which always creates a "fresh" reference cell everytime it is called. If you want to use the same reference always, you have to either make it a global variable, or you have to define it as such: let f = let count = ref 0 in fun x -> Printf.printf ... Of course, this also works (more similar to your example): let f = let count = ref 0 in let f x = Printf.printf ... in f Regards, Markus Mottl -- Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr