Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] where is my count: newbie's question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Francois Thomasset <Francois.Thomasset@i...>
Subject: [Caml-list] where is my count: newbie's question
Sorry if this is a naive question, but I can't make out the reason of the 
behavior of the following function, which is a (seemingly!) slighty 
modification of the gensym example:
# 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 = <fun>
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:
# 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 = <fun>
# fnx();;
count = 0
- : int = 1
# fnx();;
count = 1
- : int = 2



					François Thomasset.
					INRIA (A3)

Tel: +33 (1) 39-63-54-75
Fax: +33 (1) 39-63-53-30 ou +33 (1) 39-63-59-95
Email: Francois.Thomasset@inria.fr
Smail: INRIA, Rocquencourt, BP 105, 78153 Le Chesnay Cedex, France


-------------------
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