substitution model revisited
simple example
- (define (square i) (* i i))
- to evaluate (square 3)
substitute 3 for i in the body of square: (* 3 3)
so result is 9
tricky example
- to evaluate (exp 3 1), substitute 3 for x and 1 for k in body of exp
- condition becomes (= 1 0)?
- no! the k in helper’s body is a different k.
(define (exp x k)
(define (helper k s)(if (= k 0) s
(helper x (- k 1) (* x s))))
(helper k 1))