如何使用 Jane Core 对列表进行排序?

How to sort a list using Jane Core?

我正在努力实现这个

open Core
let%test _ = List.sort ~cmp:Int.compare [1;2;3] = [1;2;3]

但是失败了

61 | let%test _ = List.sort ~cmp:Int.compare [1;2;3] = [1;2;3]
                                 ^^^^^^^^^^^
Error: The function applied to this argument has type
         compare:('a -> 'a -> int) -> 'a list
This argument cannot be applied with label ~cmp

好的,我知道了。问题是谷歌搜索 ocaml 核心列表 导致我找到过时的文档。标签名称是 ~compare

所以这行得通

let%test _ = List.sort ~compare:Int.compare [1;2;3] = [1;2;3]

最新 文档:https://ocaml.janestreet.com/ocaml-core/latest/doc/ (this may not be what you're searching, you can find other versions at https://ocaml.janestreet.com/ocaml-core/)

Janestreet Core 图书馆正在随时间改变其界面。关键字参数的旧名称为 cmp,为了与库的其他部分保持一致,已更改为 compare

 let%test _ = List.sort ~compare:Int.compare [1;2;3] = [1;2;3]