From 073ea44fdbd52f9059ec715a8fdce3f74f4c490c Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 10:31:22 +0000 Subject: [PATCH] ocaml: phase 5.1 palindrome.ml baseline (two-pointer check, 4/6 inputs match) Two-pointer palindrome check: let is_palindrome s = let n = String.length s in let rec check i j = if i >= j then true else if s.[i] <> s.[j] then false else check (i + 1) (j - 1) in check 0 (n - 1) Tests on six strings: racecar = true hello = false abba = true '' = true (vacuously, i >= j on entry) 'a' = true 'ab' = false Sum = 4. Uses s.[i] <> s.[j] (string-get + structural inequality), recursive 2-arg pointer advancement, and a multi-clause if/else if/else for the three cases. 48 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/palindrome.ml | 17 +++++++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 lib/ocaml/baseline/palindrome.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 6e0e9145..cecf44fb 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -31,6 +31,7 @@ "newton_sqrt.ml": 1414, "mutable_record.ml": 10, "option_match.ml": 5, + "palindrome.ml": 4, "pascal.ml": 252, "pi_leibniz.ml": 314, "pretty_table.ml": 64, diff --git a/lib/ocaml/baseline/palindrome.ml b/lib/ocaml/baseline/palindrome.ml new file mode 100644 index 00000000..6286e886 --- /dev/null +++ b/lib/ocaml/baseline/palindrome.ml @@ -0,0 +1,17 @@ +let is_palindrome s = + let n = String.length s in + let rec check i j = + if i >= j then true + else if s.[i] <> s.[j] then false + else check (i + 1) (j - 1) + in + check 0 (n - 1) + +;; + +(if is_palindrome "racecar" then 1 else 0) + +(if is_palindrome "hello" then 1 else 0) + +(if is_palindrome "abba" then 1 else 0) + +(if is_palindrome "" then 1 else 0) + +(if is_palindrome "a" then 1 else 0) + +(if is_palindrome "ab" then 1 else 0) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 0d6bb898..63949809 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _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 — palindrome.ml baseline (two-pointer + palindrome check, 4 of 6 inputs are palindromes). is_palindrome + walks from both ends meeting in the middle, returning false on + first mismatch. Tests on six strings (racecar ✓, hello ✗, abba ✓, + "" ✓, "a" ✓, "ab" ✗) sum to 4. Uses `s.[i] <> s.[j]` (string-get + + structural inequality) and recursive 2-arg pointer advancement. + 48 baseline programs total. - 2026-05-09 Phase 5.1 — coin_change.ml baseline (min coin DP, 67¢ with [1;5;10;25] → 6 coins). Bottom-up DP: `dp[i]` = min coins for amount i. For each amount 1..target, iterate coins and