;; top-level function -- simplify an expression
(define (simplify expr)
(cond ((number? expr) expr) ; number simplifies to itself
((symbol? expr) expr) ; can't further simplify a symbol either
;; if the expression is an addition or a multiplication,
;; first simplify each argument, using a recursive call,
;; then simplify the sum or product
((plus-expr? expr)
(plus-simplify (simplify (second expr))
(simplify (third expr))))
((times-expr? expr)
(times-simplify (simplify (second expr))
(simplify (third expr))))
(else (error "unknown type of expression"))))
;; test whether an expression is an addition
(define (plus-expr? expr)
(and (list? expr) (eq? (car expr) '+)))
;; test whether an expression is a multiplication
(define (times-expr? expr)
(and (list? expr) (eq? (car expr) '*)))
Last modified: Wed Mar 29 20:29:13 PST 2000