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 NAA09840; Mon, 11 Jun 2001 13:39:48 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA10038 for caml-list@pauillac.inria.fr; Mon, 11 Jun 2001 13:39:47 +0200 (MET DST) 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 NAA09465 for ; Mon, 11 Jun 2001 13:30:15 +0200 (MET DST) Received: from pochi.inria.fr (pochi.inria.fr [128.93.8.128]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f5BBUEv12750; Mon, 11 Jun 2001 13:30:14 +0200 (MET DST) Received: (from mentre@localhost) by pochi.inria.fr (8.11.1/8.10.0) id f5BBUE921224; Mon, 11 Jun 2001 13:30:14 +0200 X-Authentication-Warning: pochi.inria.fr: mentre set sender to David.Mentre@inria.fr using -f To: Francois Thomasset Cc: caml-list@inria.fr Subject: Re: [Caml-list] where is my count: newbie's question References: <200106111021.f5BAL0L08064@ionie.inria.fr> From: David Mentre Date: 11 Jun 2001 13:30:14 +0200 In-Reply-To: <200106111021.f5BAL0L08064@ionie.inria.fr> Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.4 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Francois Thomasset writes: > # 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. It works. In your case, your are not using the proper returned function by the body of fnx: # let g = fnx 3 ;; val f : unit -> int = # g ();; x = 3 count = 0 - : int = 1 # g ();; x = 3 count = 1 - : int = 2 Said otherwise, you do not create the function 'f' (let f()=...) inside the body of fnx until you provide the first int argument. At that time, a new function corresponding to f is returned, with a new count set to zero. > Of course if I remove the int argument I retrieve the behavior I expected: > # let fnx = let count = ref 0 in > let f() = > Printf.printf "count = %d\n" !count; > count := !count + 1; !count > in f;; > val fnx : unit -> int = In that case, the *variable* fnx is assigned the function f, created immediately. -- David.Mentre@inria.fr -- http://www.irisa.fr/prive/dmentre/ Opinions expressed here are only mine. ------------------- 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