caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] where is my count: newbie's question
@ 2001-06-11 10:21 Francois Thomasset
  2001-06-11 11:26 ` Markus Mottl
  2001-06-11 11:30 ` David Mentre
  0 siblings, 2 replies; 3+ messages in thread
From: Francois Thomasset @ 2001-06-11 10:21 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-06-11 11:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-11 10:21 [Caml-list] where is my count: newbie's question Francois Thomasset
2001-06-11 11:26 ` Markus Mottl
2001-06-11 11:30 ` David Mentre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).