From 7e838bb62bcd9a653a05a0cf10e3b8f7b2d34efd Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 18:58:32 +0000 Subject: [PATCH] ocaml: phase 5.1 max_run.ml baseline (longest consecutive run, 4+1+0 = 5) Walks list keeping a previous-value reference; increments cur on match, resets to 1 otherwise. Uses 'Some y when y = x' guard pattern in match for the prev-value comparison: let max_run xs = let max_so_far = ref 0 in let cur = ref 0 in let last = ref None in List.iter (fun x -> (match !last with | Some y when y = x -> cur := !cur + 1 | _ -> cur := 1); last := Some x; if !cur > !max_so_far then max_so_far := !cur ) xs; !max_so_far Three test cases: [1;1;2;2;2;2;3;3;1;1;1] max run = 4 (the 2's) [1;2;3;4;5] max run = 1 [] max run = 0 sum = 5 Tests 'when' guard pattern in match arm + Option ref + ref-mutation sequence inside List.iter closure body. 94 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/max_run.ml | 16 ++++++++++++++++ plans/ocaml-on-sx.md | 10 ++++++++++ 3 files changed, 27 insertions(+) create mode 100644 lib/ocaml/baseline/max_run.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 452170a2..64d27e66 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -44,6 +44,7 @@ "luhn.ml": 2, "mat_mul.ml": 621, "max_path_tree.ml": 11, + "max_run.ml": 5, "mod_inverse.ml": 27, "json_pretty.ml": 24, "kadane.ml": 6, diff --git a/lib/ocaml/baseline/max_run.ml b/lib/ocaml/baseline/max_run.ml new file mode 100644 index 00000000..8e74b0d7 --- /dev/null +++ b/lib/ocaml/baseline/max_run.ml @@ -0,0 +1,16 @@ +let max_run xs = + let max_so_far = ref 0 in + let cur = ref 0 in + let last = ref None in + List.iter (fun x -> + (match !last with + | Some y when y = x -> cur := !cur + 1 + | _ -> cur := 1); + last := Some x; + if !cur > !max_so_far then max_so_far := !cur + ) xs; + !max_so_far + +;; + +max_run [1;1;2;2;2;2;3;3;1;1;1] + max_run [1;2;3;4;5] + max_run [] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index efee2b01..2c5c9359 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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-09 Phase 5.1 — max_run.ml baseline (longest consecutive + run, sum of three test cases = 4+1+0 = 5). Walks list with + `Some y when y = x` guard pattern in match for the prev-value + comparison; runs of equal elements increment cur, resets to 1 + otherwise. Tests three inputs: + [1;1;2;2;2;2;3;3;1;1;1] max run = 4 (the 2's) + [1;2;3;4;5] max run = 1 + [] max run = 0 + Sum = 5. Tests `when` guard in match arm + Option ref. 94 baseline + programs total. - 2026-05-09 Phase 5.1 — subseq_check.ml baseline (string is subsequence?, 3/5 yes). Two-pointer walk: advance `i` only on match, always advance `j`. Match if `i` reaches `n` (consumed