This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] surprising program
• Michel Levy
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2001-10-23 (21:41) From: Michel Levy Subject: [Caml-list] surprising program
```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

```