Files
rose-ash/lib/ocaml/baseline/word_freq.ml
giles 5c70747ac7
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
ocaml: phase 5.1 word_freq.ml baseline (Map.Make on String, distinct = 8)
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.
2026-05-09 08:01:21 +00:00

22 lines
449 B
OCaml

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