bool isTree(List<Object> tree) {
if ((tree is! List) | (tree.length < 1)) {
return false;
}
for (final branch in branches(tree)) {
if (!isTree(branch)) {
return false;
}
}
return true;
}
List branches(List tree) {
return tree.sublist(1);
}
Object label(List tree) {
return tree[0];
}
List tree(rootLabel, [List branches = const []]) {
for (final branch in branches) {
assert(isTree(branch));
}
return ([rootLabel] branches);
}
bool isLeaf(List tree) {
return branches(tree).isEmpty;
}
var t = tree('hey', [
tree('hello'),
tree('hum', [tree('there'), tree('hey')])
]);
If i were to remove the for loop with the assert function, and also the is_tree function, the program still returns the same results as with them, So aren't they useless?
List branches(List tree) {
return tree.sublist(1);
}
Object label(List tree) {
return tree[0];
}
List tree(rootLabel, [List branches = const []]) {
return ([rootLabel] branches);
}
bool isLeaf(List tree) {
return branches(tree).isEmpty;
}
var t = tree('hey', [
tree('hello'),
tree('hum', [tree('there'), tree('hey')])
]);
Here is the full tutorial when i am learning this https://composingprograms.com/pages/23-sequences.html#trees
CodePudding user response:
It's there to make sure all branches are trees. Without the assert you are able to write this without any errors.
var t = tree('hey', ['notATree']);
With the assert it will throw an error.