Commit 81c8fbe1 authored by POTTIER Francois's avatar POTTIER Francois

Change [Misc.best] to also work with an empty list.

parent 294515e6
......@@ -320,7 +320,9 @@ let new_encode_decode capacity =
let rec best (preferable : 'a -> 'a -> bool) (xs : 'a list) : 'a option =
match xs with
| [] ->
assert false
(* Special case: no elements at all, so no best element. This case
does not participate in the recursion. *)
None
| [x] ->
Some x
| x :: xs ->
......@@ -329,6 +331,7 @@ let rec best (preferable : 'a -> 'a -> bool) (xs : 'a list) : 'a option =
if List.for_all (preferable x) xs then
Some x
else
(* [xs] is nonempty, so the recursive call is permitted. *)
match best preferable xs with
| Some y ->
if preferable y x then
......
......@@ -178,8 +178,8 @@ val new_intern: int -> (string -> string) * (unit -> unit)
so far. *)
val new_encode_decode: int -> (string -> int) * (int -> string) * (unit -> unit)
(* If [xs] is a nonempty list and [preferable] is a partial order on elements,
then [best preferable xs] returns the best (least) element of [xs], if there
is one. Its complexity is quadratic. *)
(* If [preferable] is a partial order on elements, then [best preferable xs]
returns the best (least) element of [xs], if there is one. Its complexity
is quadratic. *)
val best: ('a -> 'a -> bool) -> 'a list -> 'a option
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