let rec max l =
match l with
| x::[]-> x
| x::xs ->
let m1 = max xs in (* first call *)
if (x > m1) then
x
else
let m2 = max xs in
m2 (* second call *)
;;
只进行一次调用是一种有效的优化,但在一般情况下并不正确。例如:
let f () =
print_endline "hello";
print_endline "hello";
3
不等同于:
let g () =
let x = print_endline "hello" in
x;
x;
3