2017-06-05 25 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:(=);;' –