ocaml: phase 5.1 word_count.ml baseline (10/10 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
14
lib/ocaml/baseline/word_count.ml
Normal file
14
lib/ocaml/baseline/word_count.ml
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user