# 0.27.0: match destructures an indexed subject, no temporary variable.use io;
defenum Tree {
Leaf{value asint},
Node{left asint, right asint}
};
# A tree as a list: a Node holds the indexes of its children.def nodes aslistof Tree init [
Tree.Node{left: 1, right: 2},
Tree.Leaf{value: 3},
Tree.Node{left: 3, right: 4},
Tree.Leaf{value: 4},
Tree.Leaf{value: 5}
];
functotal(nodes aslistof Tree, i asint) {
match ($nodes[$i]) {
whenLeaf(lf) { return$lf.value; }
whenNode(nd) { returntotal($nodes, $nd.left) + total($nodes, $nd.right); }
}
return0;
}
io.printf("sum of the leaves: %d\n", total($nodes, 0));