ocaml: phase 5.1 palindrome.ml baseline (two-pointer check, 4/6 inputs match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s

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.
This commit is contained in:
2026-05-09 10:31:22 +00:00
parent aee7226b9c
commit 073ea44fdb
3 changed files with 25 additions and 0 deletions

View File

@@ -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,

View File

@@ -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)