Loading lesson…
Level 1 · Apprentice0 XP
Add new operations to a fixed set of types without editing each type, and its modern replacement.
Step 1 / 5
ProblemAn expression tree has Number, Add and Multiply nodes. You need evaluate, print, simplify and export-to-JSON. Adding each as a method touches every node class.
← → step
Before this: Memento
🛠️ Your turn
Reading is not the skill, doing is. Try for ten minutes before you open the hint. Getting stuck first is what makes the solution stick.
1.Model a sealed expression tree with Num, Add, Mul and Neg. Write eval, show and simplify (x * 1 → x, x + 0 → x) as functions with exhaustive when, then add Sub and let the compiler guide you.