2017-06-05 27 views
0

我无法从书籍Real World OCaml运行一些代码。代码片段位于github。具体地讲,我得到一个类型的错误在第6行:OCaml Core List.Assoc.find

List.Assoc.find counts line

我使用OCaml的4.04.1和根据UTOP,的List.Assoc.find类型签名是:

('a, 'b) List.Assoc.t -> equal:('a -> 'a -> bool) -> 'a -> 'b option = <fun

注意上面代码片段中缺少的非可选参数equal。但是,根据最新(截至2017年6月)documentation对于List.Assoc.findequal参数是可选的。

equal参数的用途是什么?

回答

3

该函数通过列表查找来找到第一个元素等于您给出的值的对。如果找不到相等的元素,则返回Some (snd pair)Noneequal参数允许您在不需要内置=的情况下指定相等的定义。

为什么它值得我目前使用OCaml 4.03.0,并且我用OPAM安装了Core。所述equal参数是可选的对我来说:

# List.Assoc.find;; 
- : ('a, 'b) Core.Std.List.Assoc.t -> 
    ?equal:('a -> 'a -> bool) -> 'a -> 'b option 
+1

此代码的工作对我来说: '让ASSOC = [( “一”,1); ( “二”,2); (“three”,3)] ;;' 'List.Assoc.find assoc“two”〜equal:(=);;' –