#lang racket ; apply op to left and right arguments, assume they are numbers ; this can be tested with the number? predicate (define (op-eval op larg rarg) (cond [ (equal? op '+) (+ larg rarg) ] [ (equal? op '*) (* larg rarg) ] )) ; evaluate an infix expression tree, assuming that all the leaves ; are numbers. (define (exp-eval ex) (cond ; atoms are themselves [ (not (list? ex)) ex ] ; single element lists are the value of their first element [ (null? (rest ex)) (exp-eval (first ex)) ] ; triples require evaluation of left and right, followed by operation [ else (op-eval (second ex) (exp-eval (first ex)) (exp-eval (third ex))) ] )) (exp-eval 1) (exp-eval '1) (exp-eval '(1)) (exp-eval '(1 + 2)) (exp-eval '(3 * 4)) (exp-eval '( (1 + 2) * (3 + 4) )) ;will not work: (exp-eval '(1 + x))