Next: Rewrites Tutorial Exercise 6, Previous: Rewrites Tutorial Exercise 4, Up: Answers to Exercises [Contents][Index]
If ‘x’ is the sum ‘a + b’, then ‘nterms(x)’ must be ‘nterms(a)’ plus ‘nterms(b)’. If ‘x’ is not a sum, then ‘nterms(x)’ = 1.
[ nterms(a + b) := nterms(a) + nterms(b), nterms(x) := 1 ]
Here we have taken advantage of the fact that earlier rules always match before later rules; ‘nterms(x)’ will only be tried if we already know that ‘x’ is not a sum.