From: David Mentre <David.Mentre@inria.fr>
To: Francois Thomasset <Francois.Thomasset@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] where is my count: newbie's question
Date: 11 Jun 2001 13:30:14 +0200 [thread overview]
Message-ID: <qtlpucb5k55.fsf@pochi.inria.fr> (raw)
In-Reply-To: <200106111021.f5BAL0L08064@ionie.inria.fr>
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
prev parent reply other threads:[~2001-06-11 11:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-06-11 10:21 Francois Thomasset
2001-06-11 11:26 ` Markus Mottl
2001-06-11 11:30 ` David Mentre [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=qtlpucb5k55.fsf@pochi.inria.fr \
--to=david.mentre@inria.fr \
--cc=Francois.Thomasset@inria.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox