Skip to content

Instantly share code, notes, and snippets.

@keleshev
Created October 11, 2019 11:59
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 keleshev/e5bc4e60eb3dd46b059c0850b03de791 to your computer and use it in GitHub Desktop.
Save keleshev/e5bc4e60eb3dd46b059c0850b03de791 to your computer and use it in GitHub Desktop.
let rec compare_lengths l1 l2 =
match l1, l2 with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| _ :: l1, _ :: l2 -> compare_lengths l1 l2
let rec compare_length_with l n =
match l with
| [] ->
if n = 0 then 0 else
if n > 0 then -1 else 1
| _ :: l ->
if n <= 0 then 1 else
compare_length_with l (n-1)
module List = struct
type 'a t = 'a list
module Length = struct
type t =
| Of_list: 'a list -> t
| Of_int: int -> t
let of_list list = Of_list list
let of_int int = Of_int int
let to_int = function
| Of_list l -> List.length l
| Of_int n -> n
let compare t1 t2 =
match t1, t2 with
| Of_list l1, Of_list l2 -> compare_lengths l1 l2
| Of_list l, Of_int n -> compare_length_with l n
| Of_int n, Of_list l -> -1 * compare_length_with l n
| Of_int n, Of_int m -> compare n m
end
let length = Length.of_list
end
List.length nom_ev > List.length quote_ev
Length.(of_list nom_ev > of_list quote_ev)
List.compare_lengths nom_ev quote_ev = -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment