From dbe3c6c203b935e1fe355219c9ff4a3e469200bc Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 8 May 2026 16:11:03 +0000 Subject: [PATCH] ocaml: phase 5.1 word_count.ml baseline (10/10 pass) Uses Map.Make(StrOrd) + List.fold_left to count word frequencies; exercises the full functor pipeline with a real-world idiom: let inc_count m word = match StrMap.find_opt word m with | None -> StrMap.add word 1 m | Some n -> StrMap.add word (n + 1) m let count words = List.fold_left inc_count StrMap.empty words 10/10 baseline programs pass. --- lib/ocaml/baseline/expected.json | 3 ++- lib/ocaml/baseline/word_count.ml | 14 ++++++++++++++ plans/ocaml-on-sx.md | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 lib/ocaml/baseline/word_count.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 4d328b0d..3452637e 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -7,5 +7,6 @@ "module_use.ml": 3, "option_match.ml": 5, "quicksort.ml": 44, - "sum_squares.ml": 385 + "sum_squares.ml": 385, + "word_count.ml": 3 } diff --git a/lib/ocaml/baseline/word_count.ml b/lib/ocaml/baseline/word_count.ml new file mode 100644 index 00000000..d455225e --- /dev/null +++ b/lib/ocaml/baseline/word_count.ml @@ -0,0 +1,14 @@ +(* Baseline: word-frequency map over a list using Map.Make + List.fold_left *) +module StrOrd = struct let compare a b = compare a b end ;; +module StrMap = Map.Make(StrOrd) ;; + +let inc_count m word = + match StrMap.find_opt word m with + | None -> StrMap.add word 1 m + | Some n -> StrMap.add word (n + 1) m +;; + +let count words = List.fold_left inc_count StrMap.empty words ;; + +let m = count ["the"; "fox"; "the"; "dog"; "the"; "fox"] ;; +StrMap.find "the" m diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index bc10a8fb..b44bcece 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -386,6 +386,9 @@ the "mother tongue" closure: OCaml → SX → OCaml. This means: _Newest first._ +- 2026-05-08 Phase 5.1 — word_count.ml baseline (10/10 pass). Uses + Map.Make(StrOrd) + List.fold_left to count word frequencies; tests + the full functor pipeline with a real OCaml idiom. - 2026-05-08 Phase 6 — Map/Set extensions: iter/fold/map/filter/ is_empty + Set.union/inter (+4 tests, 422 total). Functor bodies grow naturally — all in OCaml syntax.