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
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:
@@ -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,
|
||||
|
||||
17
lib/ocaml/baseline/flatten_tree.ml
Normal file
17
lib/ocaml/baseline/flatten_tree.ml
Normal 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)
|
||||
Reference in New Issue
Block a user