diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index d521d489..9f7e6bb4 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -90,6 +90,7 @@ "lcs.ml": 4, "majority_vote.ml": 4, "manacher.ml": 7, + "lev_iter.ml": 16, "levenshtein.ml": 11, "memo_fib.ml": 75025, "mortgage.ml": 1073, diff --git a/lib/ocaml/baseline/lev_iter.ml b/lib/ocaml/baseline/lev_iter.ml new file mode 100644 index 00000000..15e2ecd6 --- /dev/null +++ b/lib/ocaml/baseline/lev_iter.ml @@ -0,0 +1,30 @@ +let lev_iter s1 s2 = + let m = String.length s1 in + let n = String.length s2 in + let prev = Array.make (n + 1) 0 in + let curr = Array.make (n + 1) 0 in + for j = 0 to n do prev.(j) <- j done; + for i = 1 to m do + curr.(0) <- i; + for j = 1 to n do + if s1.[i - 1] = s2.[j - 1] then + curr.(j) <- prev.(j - 1) + else begin + let a = prev.(j) in + let b = curr.(j - 1) in + let c = prev.(j - 1) in + let m1 = if a < b then a else b in + curr.(j) <- (if m1 < c then m1 else c) + 1 + end + done; + for j = 0 to n do prev.(j) <- curr.(j) done + done; + prev.(n) + +;; + +lev_iter "kitten" "sitting" ++ lev_iter "saturday" "sunday" ++ lev_iter "abc" "abc" ++ lev_iter "" "abcde" ++ lev_iter "intention" "execution" diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index ee42b769..5bb9b9bb 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 — lev_iter.ml baseline (iterative + Levenshtein DP, sum of 5 distances = 16). Rolling-array DP + (O(min(m,n)) space). Distances: kitten→sitting=3, saturday→ + sunday=3, abc→abc=0, ""→abcde=5, intention→execution=5; + 3+3+0+5+5=16. Complementary to existing levenshtein.ml which + uses the recursive (exponential) definition; this one is the + practical iterative form used for real ED computations. Tests + the new `<- if ... then ... else ...` rhs path twice in the + same body. 153 baseline programs total. - 2026-05-10 Phase 5.1 — binary_heap.ml baseline (array-backed binary min-heap, push 9 random values then pop in sorted order → digits concat to 123456789). Standard heap mechanics: parent