ocaml: phase 6 Array.sort/sub/append/exists/for_all/mem (+5 tests, 520 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s

Eight new Array functions, all in OCaml syntax inside runtime.sx,
delegating to the corresponding List operation on the cell's
underlying list:

  sort cmp a    -> a := List.sort cmp !a    (* mutates the cell *)
  stable_sort   = sort
  fast_sort     = sort
  append a b    -> ref (List.append !a !b)
  sub a pos n   -> ref (take n (drop pos !a))
  exists p      -> List.exists p !a
  for_all p     -> List.for_all p !a
  mem x a       -> List.mem x !a

Round-trip:
  let a = Array.of_list [3;1;4;1;5;9;2;6] in
  Array.sort compare a;
  Array.to_list a    = [1;1;2;3;4;5;6;9]
This commit is contained in:
2026-05-09 02:35:55 +00:00
parent f68ea63e46
commit 55fe1e4468
3 changed files with 50 additions and 0 deletions

View File

@@ -560,6 +560,31 @@
let to_list a = !a
let of_list xs = ref xs
let copy a = ref !a
let sort cmp a = a := List.sort cmp !a
let stable_sort = sort
let fast_sort = sort
let append a b = ref (List.append !a !b)
let sub a pos n =
let rec take xs k =
if k = 0 then []
else match xs with
| [] -> []
| h :: t -> h :: take t (k - 1)
in
let rec drop xs k =
if k = 0 then xs
else match xs with
| [] -> []
| _ :: t -> drop t (k - 1)
in
ref (take (drop !a pos) n)
let exists p a = List.exists p !a
let for_all p a = List.for_all p !a
let mem x a = List.mem x !a
let blit src si dst di n =
for k = 0 to n - 1 do
set dst (di + k) (get src (si + k))

View File

@@ -1286,6 +1286,18 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 5032)
(eval "(ocaml-run \"let a = Array.make 5 0 in for i = 0 to 4 do a.(i) <- i * i done; a.(3) + a.(4)\")")
;; ── Array.sort / sub / append / exists / mem ─────────────────
(epoch 5040)
(eval "(ocaml-run \"let a = Array.of_list [3;1;4;1;5;9;2;6] in Array.sort compare a; Array.to_list a\")")
(epoch 5041)
(eval "(ocaml-run \"let a = Array.of_list [10;20;30;40;50] in let b = Array.sub a 1 3 in Array.fold_left (+) 0 b\")")
(epoch 5042)
(eval "(ocaml-run \"let a = Array.of_list [1;2;3] in let b = Array.of_list [4;5;6] in Array.fold_left (+) 0 (Array.append a b)\")")
(epoch 5043)
(eval "(ocaml-run \"let a = Array.init 5 (fun i -> i * 2) in Array.exists (fun x -> x = 6) a\")")
(epoch 5044)
(eval "(ocaml-run \"let a = Array.of_list [1;2;3;4;5] in Array.mem 3 a\")")
EPOCHS
OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
@@ -2043,6 +2055,13 @@ check 5030 "a.(2) <- 99; a.(2) + a.(0)" '106'
check 5031 "Array.init 4 + sum a.(0..3)" '10'
check 5032 "for + a.(i) <- i*i + sum" '25'
# ── Array.sort / sub / append / exists / mem ────────────────────
check 5040 "Array.sort compare" '(1 1 2 3 4 5 6 9)'
check 5041 "Array.sub 1 3 sum" '90'
check 5042 "Array.append 6-len sum" '21'
check 5043 "Array.exists = 6" 'true'
check 5044 "Array.mem 3 [1..5]" 'true'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"