Examples · learn/10-generics · 165 of 339
task-3.solution
tin run examples/learn/10-generics/task-3.solution.tintype Tree<T> = | Leaf | Node(Tree<T>, T, Tree<T>)
// Items left to right: everything in the left branch, then this item, then the right branch.
fn items<T>(t: Tree<T>): T[] => match t {
Leaf => []
Node(left, item, right) => items(left).concat([item]).concat(items(right))
}
fn main() {
let t: Tree<int> = Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Node(Leaf, 4, Leaf)))
print(items(t))
}