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

View File

@@ -1460,6 +1460,18 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 5214)
(eval "(ocaml-run \"Option.compare compare (Some 5) (Some 3)\")")
;; ── List.equal / List.compare ────────────────────────────────
(epoch 5220)
(eval "(ocaml-run \"List.equal (=) [1;2;3] [1;2;3]\")")
(epoch 5221)
(eval "(ocaml-run \"List.equal (=) [1;2;3] [1;2;4]\")")
(epoch 5222)
(eval "(ocaml-run \"List.compare compare [1;2;3] [1;2;4]\")")
(epoch 5223)
(eval "(ocaml-run \"List.compare compare [1;2] [1;2;3]\")")
(epoch 5224)
(eval "(ocaml-run \"List.compare compare [] []\")")
EPOCHS
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -2322,6 +2334,13 @@ check 5212 "Option.equal Some 1 Some 1" 'true'
check 5213 "Option.equal Some 1 None" 'false'
check 5214 "Option.compare Some 5 Some 3" '1'
# ── List.equal / List.compare ───────────────────────────────────
check 5220 "List.equal [1;2;3] [1;2;3]" 'true'
check 5221 "List.equal [1;2;3] [1;2;4]" 'false'
check 5222 "List.compare [1;2;3] [1;2;4]" '-1'
check 5223 "List.compare [1;2] [1;2;3]" '-1'
check 5224 "List.compare [] []" '0'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"