Sort: 'a list vs sort: int list

I can not get the following example from the OCaml manual to work:

# let rec sort lst =
    match lst with
      [] -> []
    | head :: tail -> insert head (sort tail)
  and insert elt lst =
    match lst with
      [] -> [elt]
    | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail
  ;;

^-- this part works so far

# sort l;;
- : string list = ["a"; "etc."; "is"; "tale"; "told"]

^-- this is line from OCaml manual. I can’t run this line because the < in my case seems to force to int, whereas in the OCaml manual, somehow the < is polymorphic.

Question: how do I define sort/insert above so that it is polymorphic ? Thanks!

EDIT: forgot actual link: OCaml - The core language

Which module is the <= operator coming from, in this case? E.g. if you hover over it?

Stdlib.(<=) should be polymorphic: OCaml library : Stdlib

But the Base equivalents are not: Removing polymorphic compare from Core

1 Like

If I hit goto def, it jumps me to lib/base/import0.ml . I do have an open Core at the top of the file.

Is there a polymorphic (<=) in Core ?

Yeah, that’d do it.

You can locally open the Poly module

1 Like