如果我们查看 OCamlList
模块的源代码,of_seq
定义为:
let[@tail_mod_cons] rec of_seq seq = match seq () with | Seq.Nil -> [] | Seq.Cons (x1, seq) -> begin match seq () with | Seq.Nil -> [x1] | Seq.Cons (x2, seq) -> x1 :: x2 :: of_seq seq end
这是完全有道理的,除了为什么在函数内执行额外的工作而不是编写看似更简单的以下函数?
let[@tail_mod_cons] rec of_seq seq =
match seq () with
| Seq.Nil -> []
| Seq.Cons (x1, seq) -> x1 :: of_seq seq
我缺少什么洞察力才能使这有意义?
对我来说,这看起来像是简单的循环展开。即,它将循环开销分散到两倍的调用上
seq ()