diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 1b08ac96..3453f55b 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -31,6 +31,7 @@ "roman.ml": 44, "safe_div.ml": 20, "shuffle.ml": 55, + "word_freq.ml": 8, "sieve.ml": 15, "sum_squares.ml": 385, "unique_set.ml": 9, diff --git a/lib/ocaml/baseline/word_freq.ml b/lib/ocaml/baseline/word_freq.ml new file mode 100644 index 00000000..13dcac56 --- /dev/null +++ b/lib/ocaml/baseline/word_freq.ml @@ -0,0 +1,21 @@ +module StringOrd = struct + type t = string + let compare = String.compare +end + +module SMap = Map.Make (StringOrd) + +let count_words text = + let words = String.split_on_char ' ' text in + List.fold_left (fun m w -> + let n = match SMap.find_opt w m with + | Some n -> n + | None -> 0 + in + SMap.add w (n + 1) m + ) SMap.empty words + +;; + +let m = count_words "the quick brown fox jumps over the lazy dog" in +SMap.cardinal m diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 51911fb1..4f7654f3 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 — word_freq.ml baseline (Map.Make on String, + count distinct words → 8). Defines a StringOrd module + applies + Map.Make to it. Folds the input through SMap.find_opt + SMap.add to + count each word, then reports SMap.cardinal. "the quick brown fox + jumps over the lazy dog" — "the" appears twice, so 8 distinct + words. First baseline using Map.Make on a string-keyed map. 35 + baseline programs total. - 2026-05-09 Phase 6 — Either module + Hashtbl.copy (+4 tests, 602 total). Either: left, right, is_left, is_right, find_left, find_right, map_left, map_right, fold, equal, compare. Constructors