在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
Run Code Online (Sandbox Code Playgroud)
但它可以直接定义.例如:
let rec find l ~f =
match l with
| [] -> None
| hd :: tl -> if f hd then Some hd else find tl f
Run Code Online (Sandbox Code Playgroud)
使用辅助功能来定义一个函数是否有任何优势List.find
?