ocaml: phase 6 List.equal / List.compare (+5 tests, 589 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

Both take an inner predicate / comparator and walk both lists in
lockstep:

  equal eq a b     short-circuits on first mismatch
  compare cmp a b  -1 if a is a strict prefix
                    1 if b is
                    0 if both empty
                    otherwise first non-zero element comparison

Mirrors real OCaml's signatures.

  List.equal (=) [1;2;3] [1;2;3]            = true
  List.equal (=) [1;2;3] [1;2;4]            = false
  List.compare compare [1;2;3] [1;2;4]      = -1
  List.compare compare [1;2] [1;2;3]        = -1
  List.compare compare [] []                = 0
This commit is contained in:
2026-05-09 06:35:42 +00:00
parent 4d32c80a99
commit 98ba772acd
3 changed files with 44 additions and 0 deletions

View File

@@ -194,6 +194,24 @@
| Some v -> Some v
| None -> find_map f t
let rec equal eq a b =
match a with
| [] -> (match b with [] -> true | _ -> false)
| h :: t ->
(match b with
| [] -> false
| h2 :: t2 -> if eq h h2 then equal eq t t2 else false)
let rec compare cmp a b =
match a with
| [] -> (match b with [] -> 0 | _ -> -1)
| h :: t ->
(match b with
| [] -> 1
| h2 :: t2 ->
let c = cmp h h2 in
if c <> 0 then c else compare cmp t t2)
let rec combine xs ys =
match xs with
| [] -> (match ys with