Commit de9b6d8b authored by POTTIER Francois's avatar POTTIER Francois

Moved the lexicographic ordering on lists to [General].

parent e705f672
(* --------------------------------------------------------------------------- *)
(* Lists. *)
let rec take n xs =
match n, xs with
| 0, _
| _, [] ->
[]
| _, (x :: xs as input) ->
let xs' = take (n - 1) xs in
if xs == xs' then
input
else
x :: xs'
let rec drop n xs =
match n, xs with
| 0, _ ->
xs
| _, [] ->
[]
| _, _ :: xs ->
drop (n - 1) xs
let rec uniq1 cmp x ys =
match ys with
| [] ->
[]
| y :: ys ->
if cmp x y = 0 then
uniq1 compare x ys
else
y :: uniq1 cmp y ys
let uniq cmp xs =
match xs with
| [] ->
[]
| x :: xs ->
x :: uniq1 cmp x xs
let rec compare cmp xs ys =
match xs, ys with
| [], [] ->
0
| [], _ :: _ ->
-1
| _ :: _, [] ->
1
| x :: xs, y :: ys ->
let c = cmp x y in
if c <> 0 then c else compare cmp xs ys
(* --------------------------------------------------------------------------- *)
(* Streams. *)
type 'a stream =
'a head Lazy.t
and 'a head =
| Nil
| Cons of 'a * 'a stream
(* The length of a stream. *)
let rec length xs =
match Lazy.force xs with
| Nil ->
0
| Cons (_, xs) ->
1 + length xs
(* Folding over a stream. *)
let rec foldr f xs accu =
match Lazy.force xs with
| Nil ->
accu
| Cons (x, xs) ->
f x (foldr f xs accu)
(* This module offers general-purpose functions on lists and streams. *)
(* --------------------------------------------------------------------------- *)
(* Lists. *)
(* [take n xs] returns the [n] first elements of the list [xs]. It is
acceptable for the list [xs] to have length less than [n], in
which case [xs] itself is returned. *)
val take: int -> 'a list -> 'a list
(* [drop n xs] returns the list [xs], deprived of its [n] first elements.
It is acceptable for the list [xs] to have length less than [n], in
which case an empty list is returned. *)
val drop: int -> 'a list -> 'a list
(* [uniq cmp xs] assumes that the list [xs] is sorted according to the
ordering [cmp] and returns the list [xs] deprived of any duplicate
elements. *)
val uniq: ('a -> 'a -> int) -> 'a list -> 'a list
(* If [cmp] is an ordering on elements, then [compare cmp] is the
lexicographic ordering on lists. *)
val compare: ('a -> 'a -> int) -> 'a list -> 'a list -> int
(* --------------------------------------------------------------------------- *)
(* A stream is a list whose elements are produced on demand. *)
type 'a stream =
'a head Lazy.t
and 'a head =
| Nil
| Cons of 'a * 'a stream
(* The length of a stream. *)
val length: 'a stream -> int
(* Folding over a stream. *)
val foldr: ('a -> 'b -> 'b) -> 'a stream -> 'b -> 'b
......@@ -186,16 +186,7 @@ module Make
| X (N nt1), X (N nt2) ->
compare_nonterminals nt1 nt2
let rec compare_words w1 w2 =
match w1, w2 with
| [], [] ->
0
| [], _ :: _ ->
-1
| _ :: _, [] ->
1
| symbol1 :: w1, symbol2 :: w2 ->
let c = compare_symbols symbol1 symbol2 in
if c <> 0 then c else compare_words w1 w2
let compare_words w1 w2 =
General.compare compare_symbols w1 w2
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment