Rewrites Tutorial Exercise 5
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.