caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alan Schmitt <alan.schmitt@inria.fr>
To: Michel Levy <Michel.Levy@imag.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] surprising program
Date: Wed, 24 Oct 2001 07:15:37 +0200	[thread overview]
Message-ID: <20011024071537.X7383@alan-schm1p> (raw)
In-Reply-To: <3BD5E5EF.DA701A11@imag.fr>; from Michel.Levy@imag.fr on mar, oct 23, 2001 at 11:49:35 +0200

Hi,

This is an evaluation order problem. In OCaml, the evaluation order is
not specified (there is such an order but one shouldn't take it into
account, as it may change some day), and when you write:
{t with p1= !n }::numero f
there is no guarantee that the record is created before the recursive
call (and what actually happens is the recursive call, the creation of
the record, and the consing of the record on the list). Since recursive
calls are done first, n gets its final value before the records are
created, and they all have the same p1. In the second example, you use:
let a = !n' in {t with p1= a }::numero' f
so you force the evaluation of 'a' before the recursive call, which is
the Right Way (tm).

HTH,

Alan

* Michel Levy (Michel.Levy@imag.fr) wrote:
> I don't understand the behavior of the following program (ocaml 3.01)
> 
> type couple = {p1 :int ;p2:int};;
> let c1 = {p1=0;p2=10};;
> let c2 = {p1=0;p2 = 20};;
> 
> let n = ref 0;;
> let rec numero l =
>   match l with
>     | [] -> []
>     | t::f ->n:=!n+1; {t with p1= !n }::numero f;;
> 
> (* 
> # numero [c1;c2];;
> - : couple list = [{p1=2; p2=10}; {p1=2; p2=20}]
> *)
> 
> Why the fied p1 has the value 2 ?
> Why it's different with the fonction numero'
> 
> let n' = ref 0;;
> let rec numero' l =
>   match l with
>     | [] -> []
>     | t::f -> n':=!n'+1; 
> 	let a = !n' in {t with p1= a }::numero' f;;
> (* 
> # numero' [c1;c2];;
> - : couple list = [{p1=1; p2=10}; {p1=2; p2=20}]
> *)
> 
> Thank you really much for any explanation.
> 
> -- 
> Michel Levy
> 36 rue George Sand 38400 Saint Martin d'Hères
> email : Michel.Levy@imag.fr
> http://www-lsr.imag.fr/users/Michel.Levy/
> -------------------
> 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


--
The hacker: someone who figured things out and made something cool happen.
-------------------
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-10-24  5:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-23 21:49 Michel Levy
2001-10-24  5:15 ` Alan Schmitt [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=20011024071537.X7383@alan-schm1p \
    --to=alan.schmitt@inria.fr \
    --cc=Michel.Levy@imag.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).