News for hackers. Stuff that matters.
Re:Zero One Infinity Rule by teo_zero on Sunday March 19, 2023 @11:01AM (#35219653)
The principle applies to all kinds of structures. For example a common way to implement a generic tree that respects this rule is to model each node with exactly one parent and any number of children. My statement instead is that you may have legitimate reasons to want exactly one parent and from zero to two children, but not more.
Re:Zero One Infinity Rule by xigoi on Sunday March 19, 2023 @11:10AM (#35219752)
What would be the use case of such a tree?
Re:Zero One Infinity Rule by teo_zero on Monday March 20, 2023 @07:26PM (#35240342)
An Abstract Syntax Tree, where each node can have zero children (e.g. a literal value), one child (unary operator like 'Not') or two children (a binary operator like +).
Re:Zero One Infinity Rule by xigoi on Tuesday March 21, 2023 @01:36AM (#35242869)
And what exactly is the benefit of restricting such a tree to two children? Why not just allow an arbitrary amount of children and then only use it with 0,1,2? After all, you might later want to extend the language.
Re:Zero One Infinity Rule by teo_zero on Tuesday March 21, 2023 @02:18AM (#35243044)
Allowing arbitrary number of children doesn't come for free. You might need a linked list, which is a waste of memory and access time if you always just use up to two elements. What most parsers do is to provide exactly 2 slots, aptly named left and right, and use zero, one or both of them.