Homework Three -- Sample Evaluation

Comp210

Suppose the problem asked for a right-to-left evaluation of
; sub1sqrd: number --> number
; Given x, return x^2 - 2x +1.
;
(define add1-and-square
  (lambda (x)
    (+ (* x x) (* 2 x) 1)))

(+ (+ (add1-and-square (+ 3 4)) 5) (* 6 7))

Then your answer would be:
  (+ (+ (add1-and-square (+ 3 4)) 5) (* 6 7))
                                     ^^^^^^^
= (+ (+ (add1-and-square (+ 3 4)) 5) 42)
                         ^^^^^^^
= (+ (+ (add1-and-square 7) 5) 42)
         ^^^^^^^^^^^^^^^
      ; Placeholder about to evaluate to it's defn:
= (+ (+ ((lambda (x) (+ (* x x) (* 2 x) 1)) 7) 5) 42)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        
 ; Eval a func-application: re-write the lambda's body, w/ 7 subbing for x:

= (+ (+ (+ (* 7 7) (* 2 7) 1) 5) 42)
                   ^^^^^^^
= (+ (+ (+ (* 7 7) 14 1) 5) 42)
           ^^^^^^^
= (+ (+ (+ 49 14 1) 5) 42)
        ^^^^^^^^^^^
= (+ (+ 64 5) 42)
     ^^^^^^^^
= (+ 69 42)
  ^^^^^^^^^
= 111

Things to observe: