ocaml: phase 5.1 word_freq.ml baseline (Map.Make on String, distinct = 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
First baseline using Map.Make on a string-keyed map:
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
For 'the quick brown fox jumps over the lazy dog' ('the' appears
twice), SMap.cardinal -> 8.
Complements bag.ml (Hashtbl-based) and unique_set.ml (Set.Make)
with a sorted Map view of the same kind of counting problem. 35
baseline programs total.
This commit is contained in:
@@ -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,
|
||||
|
||||
21
lib/ocaml/baseline/word_freq.ml
Normal file
21
lib/ocaml/baseline/word_freq.ml
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user