Exercise · Volume 1 · 2.3-16
Exercise 2.3-16
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K8, compute by machine: find a number or an object by a large computation from the written solution |
| Section | Section 2.3 of Volume 1 |
The task
Draw trees like Fig. 21 for (a) and (b) .
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Put the operator that is applied last at the root, and the operands as children. Division and multiplication bind before subtraction and addition.
Solution
Show the solution
Each interior node is an operator. Its children are its operands in the order of the formula.
(a) The last operation is the product of and . The factor is a difference of and the quotient .
x
/ \
2 -
/ \
a /
/ \
b c
(b) The sum has three terms. Read from the left, it is .
+
/ \
+ x
/ \ / \
a b 5 c
If a sum of three terms is allowed to be one node of degree 3, the tree is the root with the three children , and with children and . Addition is associative, so a different grouping, , gives a third tree.
Check
Python3 parsed both formulas with its own parser and printed the structure as nested tuples. For (a) it printed ('Mult', 2, ('Sub', 'a', ('Div', 'b', 'c'))). For (b) it printed ('Add', ('Add', 'a', 'b'), ('Mult', 5, 'c')). Both agree with the pictures.
Remarks
A formula defines a tree only after the rules of precedence and association are fixed. In (b) the rule that addition is taken from the left gives one tree out of three.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.