Skip to content

Instantly share code, notes, and snippets.

@gallais
Last active October 26, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gallais/3ed0816fd48cfe4e2daa114913b7610a to your computer and use it in GitHub Desktop.
Save gallais/3ed0816fd48cfe4e2daa114913b7610a to your computer and use it in GitHub Desktop.
Sorted list functor
type comparison = LT | EQ | GT
module type ORDERED =
sig
type t
val compare : t -> t -> comparison
end
module type SORTEDLIST =
functor (O : ORDERED) ->
sig
type t
val from_list : O.t list -> t
end
module OrderedInt : ORDERED =
struct
type t = int
let compare a b =
if a < b then LT
else if a = b then EQ
else GT
end
module SortedList : SORTEDLIST = functor (O : ORDERED) ->
struct
type t = O.t list
let from_list : O.t list -> t =
let cmp' a b = match O.compare a b with
| LT -> -1
| EQ -> 0
| GT -> 1
in List.sort cmp'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment