【发布时间】:2014-07-24 21:36:35
【问题描述】:
在Core中,List.find是使用辅助函数定义的,如下:
let find l ~f =
let rec find_aux = function
| [] -> None
| hd :: tl -> if f hd then Some hd else find_aux tl
in
find_aux l
但是可以直接定义。例如:
let rec find l ~f =
match l with
| [] -> None
| hd :: tl -> if f hd then Some hd else find tl f
使用辅助函数来定义诸如List.find之类的函数有什么好处吗?
【问题讨论】:
-
在 Haskell 中,通过执行此类操作获得的额外内联机会非常重要。我不知道OCaml是否相同。搜索词:静态参数转换、worker-wrapper 转换