注意函数闭包本质上等同于惰性值可能会有所帮助:
lazy n : 'a Lazy.t <=> (fun () -> n) : unit -> 'a
force x : 'a <=> x () : 'a
所以'a llist 类型等价于
type 'a llist = 'a cell Lazy.t
即惰性单元格值。
根据上述定义,地图实现可能更有意义
let rec map f lst =
match force lst with
| Nil -> lazy Nil
| Cons (hd,tl) -> lazy (Cons (f hd, map f tl))
将其转换回闭包:
let rec map f lst =
match lst () with
| Nil -> (fun () -> Nil)
| Cons (hd,tl) -> (fun () -> Cons (f hd, map f tl))
与追加类似
let rec append a b =
match force a with
| Nil -> b
| Cons (hd,tl) -> lazy (Cons (hd, append tl b))
变成
let rec append a b =
match a () with
| Nil -> b
| Cons (hd,tl) -> (fun () -> Cons (hd, append tl b))
我通常更喜欢使用 lazy 语法,因为它更清楚发生了什么。
另外请注意,延迟挂起和关闭并不完全等效。例如,
let x = lazy (print_endline "foo") in
force x;
force x
打印
foo
而
let x = fun () -> print_endline "foo" in
x ();
x ()
打印
foo
foo
不同之处在于force只计算一次表达式的值。