小编jpv*_*aza的帖子

在OCaml中,为什么Core的List.find中有辅助功能?

在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

ocaml list

5
推荐指数
1
解决办法
265
查看次数

标签 统计

list ×1

ocaml ×1