From 667dfcfd7c024321d717e9f93ed1ab1c8cbe67eb Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 15:02:13 +0000 Subject: [PATCH] ocaml: phase 5.1 hist.ml baseline (Hashtbl int histogram, total * max = 75) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three small functions: hist xs build a Hashtbl of count-by-value max_value h Hashtbl.fold to find the max bin total h Hashtbl.fold to sum all bins For the 15-element list [1;2;3;1;4;5;1;2;6;7;1;8;9;1;0]: total = 15 max_value = 5 (the number 1 appears 5 times) product = 75 Companion to bag.ml (string keys) and frequency.ml (char keys) — same Hashtbl.fold + Hashtbl.find_opt pattern, exercised on int keys this time. 71 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/hist.ml | 21 +++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/ocaml/baseline/hist.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 4b719c8a..c158e019 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -31,6 +31,7 @@ "group_consec.ml": 53, "hailstone.ml": 111, "hanoi.ml": 1023, + "hist.ml": 75, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, "list_ops.ml": 30, diff --git a/lib/ocaml/baseline/hist.ml b/lib/ocaml/baseline/hist.ml new file mode 100644 index 00000000..fa826bd6 --- /dev/null +++ b/lib/ocaml/baseline/hist.ml @@ -0,0 +1,21 @@ +let hist xs = + let h = Hashtbl.create 8 in + List.iter (fun x -> + let n = match Hashtbl.find_opt h x with + | Some n -> n + 1 + | None -> 1 + in + Hashtbl.replace h x n + ) xs; + h + +let max_value h = + Hashtbl.fold (fun _ v acc -> if v > acc then v else acc) h 0 + +let total h = + Hashtbl.fold (fun _ v acc -> acc + v) h 0 + +;; + +let h = hist [1;2;3;1;4;5;1;2;6;7;1;8;9;1;0] in +total h * max_value h diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index f47ef114..6d6a3f86 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — hist.ml baseline (Hashtbl-based int + histogram, total * max = 75). Three small functions: hist builds + the count table, max_value finds the maximum bin, total sums all + bins. For a 15-element list with a top frequency of 5 (the + number 1), product = 75. Companions to bag.ml (string keys) and + frequency.ml (char keys) — same Hashtbl.fold + Hashtbl.find_opt + pattern, exercised on int keys this time. 71 baseline programs + total. - 2026-05-09 Phase 5.1 — mortgage.ml baseline (monthly mortgage payment formula, 200k @ 5% / 30y → $1073). Manual `(1+r)^n` computed via for-loop because `Float.pow` may not be available