ocamlocaml-core

How to sort a list using Jane Core?


I'm trying to achieve this

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

But it fails with

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

Okay I find out. The problem was that googling for ocaml core list lead me to outdated docs. The label name is ~compare

So this works

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

The latest docs: 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/)


Solution

  • The Janestreet Core library is changing its interface with time. The old name of the keyword parameter was cmp and it was changed to compare for the consistency with the other parts of the library,

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