From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id HAA11330; Wed, 24 Oct 2001 07:21:46 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA11347 for ; Wed, 24 Oct 2001 07:21:45 +0200 (MET DST) Received: from alan-schm1p.inria.fr ([217.11.167.176]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f9O5Lfj12422 for ; Wed, 24 Oct 2001 07:21:42 +0200 (MET DST) Received: by alan-schm1p.inria.fr (Postfix, from userid 11207) id A79CC23646; Wed, 24 Oct 2001 07:15:37 +0200 (CEST) Date: Wed, 24 Oct 2001 07:15:37 +0200 From: Alan Schmitt To: Michel Levy Cc: caml-list@inria.fr Subject: Re: [Caml-list] surprising program Message-ID: <20011024071537.X7383@alan-schm1p> Mail-Followup-To: Michel Levy , caml-list@inria.fr References: <3BD5E5EF.DA701A11@imag.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable User-Agent: Mutt/1.2.5i In-Reply-To: <3BD5E5EF.DA701A11@imag.fr>; from Michel.Levy@imag.fr on mar, oct 23, 2001 at 11:49:35 +0200 X-Editor: Vim http://vim.sf.net/ X-Info: http://pauillac.inria.fr/~aschmitt/ X-Operating-System: Linux/2.4.7 (i686) X-Uptime: 7:10am up 12 days, 1:16, 1 user, load average: 0.18, 0.20, 0.12 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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=3D !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 =3D !n' in {t with p1=3D 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) >=20 > type couple =3D {p1 :int ;p2:int};; > let c1 =3D {p1=3D0;p2=3D10};; > let c2 =3D {p1=3D0;p2 =3D 20};; >=20 > let n =3D ref 0;; > let rec numero l =3D > match l with > | [] -> [] > | t::f ->n:=3D!n+1; {t with p1=3D !n }::numero f;; >=20 > (*=20 > # numero [c1;c2];; > - : couple list =3D [{p1=3D2; p2=3D10}; {p1=3D2; p2=3D20}] > *) >=20 > Why the fied p1 has the value 2 ? > Why it's different with the fonction numero' >=20 > let n' =3D ref 0;; > let rec numero' l =3D > match l with > | [] -> [] > | t::f -> n':=3D!n'+1;=20 > let a =3D !n' in {t with p1=3D a }::numero' f;; > (*=20 > # numero' [c1;c2];; > - : couple list =3D [{p1=3D1; p2=3D10}; {p1=3D2; p2=3D20}] > *) >=20 > Thank you really much for any explanation. >=20 > --=20 > Michel Levy > 36 rue George Sand 38400 Saint Martin d'H=E8res > 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.f= r/FAQ/ > To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.in= ria.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