[3/10/99] =============================================================================== * A "heavy" RSM example: Use the first repeated definition [simplest - has the same form of (lambda (x) (lambda (y) ...)), more on these differences in the next section]: And demonstrate the evaluation of: -------------------- (define cube (lambda (x) (* x x x))) (((repeated deriv 2) cube) 3) -------------------- And for the really bored - try to evaluate something like -------------------- (((repeated deriv 2) (repeated (compose cube square) 2)) 3) -------------------- =============================================================================== * Take these definitions of nth-deriv (by now you should be able to know that they all work properly): -------------------- (define (nth-deriv f n) (lambda (x) (if (= n 0) (f x) ((nth-deriv (deriv f) (- n 1)) x)))) (define (nth-deriv f n) (if (= n 0) f (lambda (x) ((nth-deriv (deriv f) (- n 1)) x)))) (define (nth-deriv f n) (if (= n 0) f (deriv (nth-deriv f (- n 1))))) (define (nth-deriv f n) (if (= n 0) f (nth-deriv (deriv f) (- n 1)))) -------------------- What are the differences between them? 1 & 2 - very similar, 1 is doing a little less when applied - the first if is evaluated in the output function. 3 & 4 - are also very similar - 4 is the tail-recursive version of 3 A big difference is that 1/2 are deferring the computation of the nth-derivative but 3/4 construct the whole thing immediately. This is the same difference between the two repeated versions that we saw last time: -------------------- (define identity (lambda (x) x)) (define (repeated1 f n) (if (zero? n) identity (lambda (x) ((repeated f (- n 1)) (f n))))) (define (repeated2 f n) (letrec ((iter (lambda (n f-acc) (if (zero? n) f-acc (iter (- n 1) (lambda (x) (f (f-acc x))))))) (iter n identity)))) -------------------- Define: -------------------- (define inc (lambda (x) (+ x 1))) (define inc3a (repeated1 inc 3)) (define inc3b (repeated2 inc 3)) -------------------- Compare the evaluation of: -------------------- (inc3a 10) -------------------- and -------------------- (inc3b 10) -------------------- You should now know why (repeated2 inc 1000) is better than the same using repeated1. ===============================================================================