这是我的实现map:
let rec map f lst =
match lst with
| [] -> []
| hd :: tl -> f hd :: map f tl
Run Code Online (Sandbox Code Playgroud)
我尝试像这样运行它:
(* Print the given int, then return the given int. *)
let print_id n =
print_int n;
print_newline ();
n
let () = ignore (map print_id [1; 2; 3])
Run Code Online (Sandbox Code Playgroud)
虽然map print_id [1; 2; 3]返回[1; 2; 3],但上面的代码打印:
3
2
1
Run Code Online (Sandbox Code Playgroud)
看来列表正在以相反的顺序处理!怎么了?
OCaml 不保证表达式求值的顺序。所以这个表达式:
f hd :: map f tl
Run Code Online (Sandbox Code Playgroud)
允许map在调用 to 之前评估对 的调用f。
您可以使用以下let方式来保证评估订单:
let x = f hd in
x :: map f tl
Run Code Online (Sandbox Code Playgroud)