diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 65062630..95a8bbe7 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -57,6 +57,7 @@ "run_length.ml": 11, "safe_div.ml": 20, "shuffle.ml": 55, + "subset_sum.ml": 8, "word_freq.ml": 8, "zip_unzip.ml": 1000, "sieve.ml": 15, diff --git a/lib/ocaml/baseline/subset_sum.ml b/lib/ocaml/baseline/subset_sum.ml new file mode 100644 index 00000000..5f65db14 --- /dev/null +++ b/lib/ocaml/baseline/subset_sum.ml @@ -0,0 +1,10 @@ +let rec count_subsets xs target = + match xs with + | [] -> if target = 0 then 1 else 0 + | x :: rest -> + count_subsets rest target + + count_subsets rest (target - x) + +;; + +count_subsets [1; 2; 3; 4; 5; 6; 7; 8] 10 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index face6a09..880975f1 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -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 — subset_sum.ml baseline (count subsets of + [1..8] summing to 10 = 8). Pure recursion: at each element, take + it or don't. Base case: target=0 → 1, target≠0 → 0. 2^8 = 256 + recursive calls. The 8 subsets are: 1+2+3+4, 1+2+7, 1+3+6, 1+4+5, + 1+9 (no), 2+3+5, 2+8, 3+7, 4+6, 1+2+3+4. Verified count = 8. + Tests doubly-recursive list traversal pattern. 65 baseline programs + total. - 2026-05-09 Phase 5.1 — hailstone.ml baseline (Collatz length, starting from 27 → 111 steps to reach 1). Iterative while-loop applies `n / 2` if even, `3n + 1` if odd, counting steps. 27 is