diff --git a/lib/ocaml/baseline/anagram_check.ml b/lib/ocaml/baseline/anagram_check.ml new file mode 100644 index 00000000..641cc0e6 --- /dev/null +++ b/lib/ocaml/baseline/anagram_check.ml @@ -0,0 +1,23 @@ +let to_counts s = + let counts = Array.make 256 0 in + for i = 0 to String.length s - 1 do + let c = Char.code s.[i] in + counts.(c) <- counts.(c) + 1 + done; + counts + +let same_counts a b = + let result = ref true in + for i = 0 to 255 do + if a.(i) <> b.(i) then result := false + done; + !result + +let is_anagram s t = same_counts (to_counts s) (to_counts t) + +;; + +(if is_anagram "listen" "silent" then 1 else 0) + +(if is_anagram "hello" "world" then 1 else 0) + +(if is_anagram "anagram" "nagaram" then 1 else 0) + +(if is_anagram "abc" "abcd" then 1 else 0) diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index bb5b75fe..e9ad3ee5 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -1,5 +1,6 @@ { "ackermann.ml": 125, + "anagram_check.ml": 2, "anagrams.ml": 3, "bag.ml": 3, "bigint_add.ml": 28, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index adcdbb15..3a86cdb2 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 — anagram_check.ml baseline (char-frequency + array, 2/4 pairs are anagrams). to_counts builds a 256-slot int + array of character frequencies. same_counts compares two arrays + element-by-element. is_anagram = same_counts on both. listen ~ + silent ✓, hello ≠ world, anagram ~ nagaram ✓, abc ≠ abcd → 2. + Exercises Array.make + arr.(i) + arr.(i) <- v + nested for loops + + Char.code + s.[i]. 57 baseline programs total. - 2026-05-09 Phase 5.1 — exception_user.ml baseline (user-defined exception with int payload, 4+5+7+10 = 26). Defines `exception Negative of int`, `safe_sqrt` raises it on negative input, and