caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] surprising program
@ 2001-10-23 21:49 Michel Levy
  2001-10-24  5:15 ` Alan Schmitt
  0 siblings, 1 reply; 2+ messages in thread
From: Michel Levy @ 2001-10-23 21:49 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2001-10-24  5:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-23 21:49 [Caml-list] surprising program Michel Levy
2001-10-24  5:15 ` Alan Schmitt

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