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

* Re: [Caml-list] where is my count: newbie's question
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Mottl @ 2001-06-11 11:26 UTC (permalink / raw)
  To: Francois Thomasset; +Cc: caml-list

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

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


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

* Re: [Caml-list] where is my count: newbie's question
  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
  1 sibling, 0 replies; 3+ messages in thread
From: David Mentre @ 2001-06-11 11:30 UTC (permalink / raw)
  To: Francois Thomasset; +Cc: caml-list

Francois Thomasset <Francois.Thomasset@inria.fr> 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 = <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.

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 = <fun>
# 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 = <fun>

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


^ 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).