OCaml 中的标签 ~f
label ~f in OCaml
我在OCaml工作,我使用的环境是Eclipe Mars。当我尝试使用 List.iter [1.;3.;2.;-7.;4.;5.] ~f:(fun x -> update rsum x);;
时,它给出了一个错误,指出 Error: The function applied to this argument has type 'a list -> unit
This argument cannot be applied with label ~f#
关键是这是一本书中的一个示例,非常适合他们,但是当我尝试使用 ~f:
时,我经常遇到这个错误。有人可以向我解释为什么这不起作用吗?
如果我知道如何进行这项工作也会有所帮助。
代字号引入了称为 labelled argument 的功能。 OCaml 标准库有模块 List,其中函数声明时没有带标签的参数,模块 ListLabels 使用它们。
我的假设是,当作者使用另一个名为 Core
的 standard
库时,您正在阅读 Real World OCaml。这个库有它自己的 List 模块,这个库经常使用带标签的参数。所以,您可能忘记将这个库加载到顶层。您可能需要解释 Eclispe 如何加载正确的库或在源文件顶部写入 [=12=]。
标准列表模块不支持带标签的参数。标准 ListLabels 模块 支持带标签的参数。
# List.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
Error: The function applied to this argument has type 'a list -> unit
This argument cannot be applied with label ~f
# ListLabels.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
1
2
3
- : unit = ()
如果您是从教程或书籍中学习,则需要确保您使用的是与它们相同的模块。
我在OCaml工作,我使用的环境是Eclipe Mars。当我尝试使用 List.iter [1.;3.;2.;-7.;4.;5.] ~f:(fun x -> update rsum x);;
时,它给出了一个错误,指出 Error: The function applied to this argument has type 'a list -> unit
This argument cannot be applied with label ~f#
关键是这是一本书中的一个示例,非常适合他们,但是当我尝试使用 ~f:
时,我经常遇到这个错误。有人可以向我解释为什么这不起作用吗?
如果我知道如何进行这项工作也会有所帮助。
代字号引入了称为 labelled argument 的功能。 OCaml 标准库有模块 List,其中函数声明时没有带标签的参数,模块 ListLabels 使用它们。
我的假设是,当作者使用另一个名为 Core
的 standard
库时,您正在阅读 Real World OCaml。这个库有它自己的 List 模块,这个库经常使用带标签的参数。所以,您可能忘记将这个库加载到顶层。您可能需要解释 Eclispe 如何加载正确的库或在源文件顶部写入 [=12=]。
标准列表模块不支持带标签的参数。标准 ListLabels 模块 支持带标签的参数。
# List.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
Error: The function applied to this argument has type 'a list -> unit
This argument cannot be applied with label ~f
# ListLabels.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
1
2
3
- : unit = ()
如果您是从教程或书籍中学习,则需要确保您使用的是与它们相同的模块。