Last active
October 26, 2017 13:39
-
-
Save gallais/3ed0816fd48cfe4e2daa114913b7610a to your computer and use it in GitHub Desktop.
Sorted list functor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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