Comp210
; 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))
(+ (+ (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
(+ 2 (* 4 5) 6) ^^^^^^^ (+ 2 20 6) 28
(* 6 7)
.
What actually happens is that first 7
is evaluated (to 7). Then, 6
is evaluated (to 6);
next the placeholder *
is evaluated to its value
(the primitive function multiplication;
writing this abstract entity is
difficult, but conventionally we write this with "*").
All this, before we actually apply the primitive.
For your hw (as in this example), you can skip the evaluation of values
(like 6
and 7
),
and placeholders for primitives (*
).