List 列出过滤器,然后列出OCaml中的映射

List 列出过滤器,然后列出OCaml中的映射,list,ocaml,ocaml-core,List,Ocaml,Ocaml Core,在OCaml中,如何在列表映射之前应用列表过滤器?我正在尝试管道操作员,但没有成功: let r infile = match List.tl (In_channel.read_lines infile ) with | None -> [] | Some body -> List.filter body ~f:(fun line -> true) |> List.map body ~f:(fun line -> match split_on_com

在OCaml中,如何在列表映射之前应用列表过滤器?我正在尝试管道操作员,但没有成功:

let r infile =
match List.tl (In_channel.read_lines infile ) with
| None -> []
| Some body ->
  List.filter body ~f:(fun line -> true)
  |> List.map body ~f:(fun line ->
    match split_on_comma line with
    | _ :: _ :: num :: street :: unit :: city :: _ :: region :: _ ->
      String.strip (num ^ " " ^ addr_case street ^ ", " ^ addr_case city ^ " " ^ region)
    | _ -> assert false)
乌托普给了我:

“此表达式具有类型字符串列表 但表达式的类型应为string list->“a”


我知道List.filter目前不起任何作用。我只是想在List.map之前使用它。标准的OCaml List.filter没有
~f:
参数。很可能您正在使用Core

我现在没有设置核心,所以无法测试。但是一个可能的问题是,您正在
列表.map
调用中使用
body
。我想你应该把它删掉。您想处理
列表.过滤器
表达式的结果。您不想处理body,它是匹配的原始值

下面是使用OCaml标准库版本函数的类似表达式:

# ListLabels.filter [1; 2; 3; 4]
      ~f: (fun x -> x mod 2 = 0) |>
  ListLabels.map ~f: (fun x -> x + 10) ;;
- : int list = [12; 14]

标准OCaml List.filter没有
~f:
参数。很可能您正在使用Core

我现在没有设置核心,所以无法测试。但是一个可能的问题是,您正在
列表.map
调用中使用
body
。我想你应该把它删掉。您想处理
列表.过滤器
表达式的结果。您不想处理body,它是匹配的原始值

下面是使用OCaml标准库版本函数的类似表达式:

# ListLabels.filter [1; 2; 3; 4]
      ~f: (fun x -> x mod 2 = 0) |>
  ListLabels.map ~f: (fun x -> x + 10) ;;
- : int list = [12; 14]