OCamlの評価順序

OCamlの2項演算子の評価順序は右から左だった.

# let x = ref 3 in (let () = x := 5 in !x) + !x;;
- : int = 8
# let x = ref 3 in !x + (let () = x := 5 in !x);;
- : int = 10

関数の引数の評価順序も同様

# let f x y = x + y;;
val f : int -> int -> int
# let x = ref 3 in f (x := 5; !x) !x;;
- : int = 8
# let x = ref 3 in f !x (x := 5; !x);;
- : int = 10

組型も同様

# let g (x,y) = x + y;;
val g : int * int -> int
# let x = ref 3 in g ((x := 5; !x), !x);;
- : int = 8
# let x = ref 3 in g (!x, (x := 5; !x));;
- : int = 10

ところが,letの評価順序は左から右だ!

# let x = ref 3 in let v1 = (x := 5; !x) and v2 = !x in v1 + v2;;
- : int = 10
# let x = ref in let v1 = !x and v2 = (x := 5; !x) in v2 + v2;;
- : int = 8

うーむ.