ocaml: phase 6 List.sort + compare (+7 tests, 339 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

compare is a host builtin returning -1/0/1 (Stdlib.compare semantics)
deferred to host SX </>. List.sort is insertion-sort in OCaml: O(n²)
but works correctly. List.stable_sort = sort.

Tested: ascending int sort, descending via custom comparator (b - a),
empty list, string sort.
This commit is contained in:
2026-05-08 12:59:50 +00:00
parent 812aa75d43
commit 202ea9cf5f
4 changed files with 54 additions and 2 deletions

View File

@@ -67,6 +67,14 @@
(list "print_string" (fn (s) (begin (print s) nil)))
(list "print_endline" (fn (s) (begin (println s) nil)))
(list "print_int" (fn (i) (begin (print (str i)) nil)))
;; Polymorphic compare — returns negative / 0 / positive like
;; OCaml's Stdlib.compare. Defers to host SX `<` and `>`.
(list "compare"
(fn (a) (fn (b)
(cond
((< a b) -1)
((> a b) 1)
(else 0)))))
;; Hashtbl primitives (one-element list cell holding a dict).
;; Keys are coerced to strings via `str` so any value type works
;; as a key (matches Hashtbl's polymorphic-key semantics).