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