diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 140edeea..037e7084 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -24,6 +24,7 @@ "grep_count.ml": 3, "hanoi.ml": 1023, "fizzbuzz.ml": 57, + "flatten_tree.ml": 28, "list_ops.ml": 30, "mat_mul.ml": 621, "json_pretty.ml": 24, diff --git a/lib/ocaml/baseline/flatten_tree.ml b/lib/ocaml/baseline/flatten_tree.ml new file mode 100644 index 00000000..8572dd4b --- /dev/null +++ b/lib/ocaml/baseline/flatten_tree.ml @@ -0,0 +1,17 @@ +type 'a tree = Leaf of 'a | Node of 'a tree list + +let rec flatten t = + match t with + | Leaf x -> [x] + | Node ts -> List.concat (List.map flatten ts) + +;; + +let t = Node [ + Leaf 1; + Node [Leaf 2; Leaf 3]; + Node [Node [Leaf 4]; Leaf 5; Leaf 6]; + Leaf 7 +] +in +List.fold_left (+) 0 (flatten t) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 377f0165..e185bb34 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 — flatten_tree.ml baseline (parametric ADT + flatten, sum 1..7 = 28). Defines `type 'a tree = Leaf of 'a | + Node of 'a tree list` then `flatten` recursively expands using + `List.concat (List.map flatten ts)`. Tree has 3 levels of + nesting; flattens to [1;2;3;4;5;6;7]. Tests parametric ADT, mutual + recursion via map+self, List.concat from runtime. 55 baseline + programs total. - 2026-05-09 Phase 5.1 — gcd_lcm.ml baseline (Euclidean gcd + lcm, 12 + 12 + 36 = 60). Two-line baseline: `let rec gcd a b = if b = 0 then a else gcd b (a mod b)` + `let lcm a b = a * b / gcd a b`.