Com S 342 meeting -*- Outline -*- * recursion remembered to do examples both in scheme and in Java. ** square root by Newton's method (1.1.7) start with a number and guess (define (sqrt-iter guess x) (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x))) improve by averaging it with the quotient of the number and the old guess (define (improve guess x) (average guess (/ x guess))) (define (good-enough? guess x) (< (abs (- (square guess) x)) 0.00001)) (define (sqrt x) (sqrt-iter 1.0 x)) relate this code to the language we designed in class. now do this in Java. Show how to do the testing. Be sure to make the nonessential methods private in the Java code. Then go back to the Scheme code and show how to use local definitions for the private methods. ** processes generated by procedures (1.2) *** linear vs. tail recursion do factorial and fact-iter in Scheme for tail recursion: count n down to 0, accumulating the product n product 6 1 5 6 4 30 3 120 2 360 1 720 0 720 another plan: is to count up to n, use "product" as accumulator this is the one in the book product counter max-count 1 1 6 1 2 6 2 3 6 6 4 6 24 5 6 120 6 6 720 7 6 in Java show how to do this using a while loop. Relate while loop and tail recursion. Discuss tail recursion optimization. *** tree recursion (1.2.2) **** Fibonacci numbers Fibonacci numbers using tree recursion in Scheme. Note: Fibonacci numbers arise as follows: imagine cellss take a generation after they are "born" to be able to generate kids, but then can have a kid every generation thereafter start with one cell, in next generation, still have 1, but after that have 2, then parent has one but not kid, so 3 total 1, 1, 2, 3, 5, 8, 13, 21, ... write code in Scheme (work from examples). (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2)))))) This is very slow!!! Tail recursive version in Scheme (do plan first) (define (fib n) (fib-iter 1 0 n)) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1)))) compare speed **** counting change, p. 40 how many ways are there to make change for n pennies? talk about one step and rest of journey idea (ways using all but first kind, and ways to change rest of it) also have to worry about base cases work from small examples (define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50))) *** logarithmic processes (can omit if short of time) **** exponentiation (1.2.4) have them do exponentiation it with linear recursion show how it works by repeated multiplication and squaring **** greatest common divisor (1.2.5) idea is based on that gcd(a, b) = gcd(b, a rem b)