【问题标题】:How to sort a list using Jane Core?如何使用 Jane Core 对列表进行排序?
【发布时间】:2019-12-18 10:37:21
【问题描述】:

我正在努力实现这一目标

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/(这可能不是您要搜索的,您可以在 https://ocaml.janestreet.com/ocaml-core/ 找到其他版本)

【问题讨论】:

  • 您可能希望将您的编辑移至未来读者的自我回答。
  • 我已经将您自己的答案(稍微改写)变成了正确的答案,以便 SO 的其他用户也可以从中受益。

标签: ocaml ocaml-core


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多