Loading...
intermediate
Problem 10 • Step 02
Compute
The expression tree is just data. It has no meaning until you give it one.
Write compute — a language function that walks the tree and produces a number. Check the tag with head("e"), then handle each case:
"lit"— return the value:tail("e")"add"— recursively compute both children, add the results"mul"— recursively compute both children, multiply the results
The children of an add or mul node are stored as a pair in tail("e"). The left child is head(tail("e")), the right child is tail(tail("e")).
Use iff to branch on the tag: iff(eq(head("e"), str("lit")), ..., iff(eq(head("e"), str("add")), ..., ...)).