caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Francois Thomasset <Francois.Thomasset@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] where is my count: newbie's question
Date: Mon, 11 Jun 2001 13:26:08 +0200	[thread overview]
Message-ID: <20010611132608.B8432@miss.wu-wien.ac.at> (raw)
In-Reply-To: <200106111021.f5BAL0L08064@ionie.inria.fr>; from Francois.Thomasset@inria.fr on Mon, Jun 11, 2001 at 12:21:00 +0200

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


  reply	other threads:[~2001-06-11 11:26 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 [this message]
2001-06-11 11:30 ` David Mentre

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=20010611132608.B8432@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --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;
as well as URLs for NNTP newsgroup(s).