previous | index | next

Evaluating Expression Trees

Wanted: A procedure evaluate that can evaluate any infix expression:
     (evaluate (make-constant 13)) ⇒ 13

     (evaluate (make-expr (make-constant 3) '- (make-constant 5))) ⇒ -2
Note that since we know the ADT representation of expressions, we can "cheat" and give evaluate the representations directly:
     (evaluate 13) ⇒ 13

     (evaluate '(3 - 5)) ⇒ -2

     (evaluate '(1 + (2 * (3 - 5)))) ⇒ -3
The latter is shorthand for:
     (evaluate (make-expr (make-constant 1) '+
                 (make-expr (make-constant 2) '*
                   (make-expr (make-constant 3) '- (make-constant 5))))) ⇒ -3

previous | index | next