ocaml: phase 5.1 flatten_tree.ml baseline (parametric ADT flatten, sum 1..7 = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

Defines a parametric tree:

  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)

Test tree has 3 levels of nesting:

  Node [Leaf 1; Node [Leaf 2; Leaf 3];
        Node [Node [Leaf 4]; Leaf 5; Leaf 6];
        Leaf 7]

flattens to [1;2;3;4;5;6;7] -> sum = 28.

Tests parametric ADT, mutual recursion via map+self, List.concat.

55 baseline programs total.
This commit is contained in:
2026-05-09 11:52:19 +00:00
parent cca3a28206
commit 027678f31e
3 changed files with 25 additions and 0 deletions

View File

@@ -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,

View File

@@ -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)

View File

@@ -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`.