From ca34cede8864d235ed6394d36c6454b7d297a652 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 23:58:50 +0000 Subject: [PATCH] ocaml: phase 5.1 next_greater.ml baseline (monotonic stack, sum 153) Right-to-left monotonic stack for next-greater-element: for i = n - 1 downto 0 do while (match !stack with [] -> false | h :: _ -> h <= arr.(i)) do stack := List.tl !stack done; (match !stack with | [] -> res.(i) <- -1 | h :: _ -> res.(i) <- h); stack := arr.(i) :: !stack done For [4; 5; 2; 25; 7; 8; 1; 30; 12]: results: [5; 25; 25; 30; 8; 30; 30; -1; -1] sum of non-negative = 5+25+25+30+8+30+30 = 153 Tests stack as ref list with match-driven peek, match-as-bool in while-guard, inline parenthesized match driving <-. 169 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/next_greater.ml | 23 +++++++++++++++++++++++ plans/ocaml-on-sx.md | 9 +++++++++ 3 files changed, 33 insertions(+) create mode 100644 lib/ocaml/baseline/next_greater.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 864e52a3..d06b991b 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -110,6 +110,7 @@ "module_use.ml": 3, "monotonic.ml": 4, "newton_sqrt.ml": 1414, + "next_greater.ml": 153, "next_permutation.ml": 119, "number_words.ml": 106, "mutable_record.ml": 10, diff --git a/lib/ocaml/baseline/next_greater.ml b/lib/ocaml/baseline/next_greater.ml new file mode 100644 index 00000000..9c09fb3e --- /dev/null +++ b/lib/ocaml/baseline/next_greater.ml @@ -0,0 +1,23 @@ +let next_greater arr = + let n = Array.length arr in + let res = Array.make n (-1) in + let stack = ref [] in + for i = n - 1 downto 0 do + while (match !stack with [] -> false | h :: _ -> h <= arr.(i)) do + stack := List.tl !stack + done; + (match !stack with + | [] -> res.(i) <- -1 + | h :: _ -> res.(i) <- h); + stack := arr.(i) :: !stack + done; + res + +;; + +let r = next_greater [| 4; 5; 2; 25; 7; 8; 1; 30; 12 |] in +let sum = ref 0 in +for i = 0 to Array.length r - 1 do + if r.(i) >= 0 then sum := !sum + r.(i) +done; +!sum diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 2b0d06f3..798a952a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-10 Phase 5.1 — next_greater.ml baseline (monotonic stack + for next-greater-element over [4;5;2;25;7;8;1;30;12], sum of + successors = 153). Right-to-left scan with a stack that pops + elements ≤ current. Result: [5,25,25,30,8,30,30,-1,-1]; sum of + non-negative entries = 5+25+25+30+8+30+30 = 153. Tests stack + encoded as `ref list` with match-driven peek, `match … with [] + -> false | h :: _ -> …` boolean coercion in `while` guard, + inline match-statement on rhs of `<-` (paren-wrapped for parse). + 169 baseline programs total. - 2026-05-10 Phase 5.1 — fenwick_tree.ml baseline (Binary Indexed Tree over [1;3;5;7;9;11;13;15], total + after = 228). Initial prefix_sum n = 64; after +100 at index 1, prefix_sum n = 164;