Transcript slides
Lecture 5: Recursion Beware the Lizards! CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/~evans Menu • PS1 Comments • FIBO Example 25 January 2002 CS 200 Spring 2002 2 Problem Set 1 • Without Evaluation Rules, Question 2 was “guesswork” • Now you know the Evaluation Rules, Question 2 should be obvious 25 January 2002 CS 200 Spring 2002 3 2d (100 + 100) Evaluation Rule 3. Application. a. Evaluate all the subexpressions 100 <primitive:+> 100 b. Apply the value of the first subexpression to the values of all the other subexpressions Error: 100 is not a procedure, we only have apply rules for procedures! 25 January 2002 CS 200 Spring 2002 4 2h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression1. Otherwise, the value of the if expression is the value of Expression2. Evaluate (not "cookies") 25 January 2002 CS 200 Spring 2002 5 Evaluate (not "cookies") Evaluation Rule 3. Application. a. Evaluate all the subexpressions <primitive:not> “cookies” The quotes really matter here! Without them what would cookies evaluate to? b. Apply the value of the first subexpression to the values of all the other subexpressions (not v) evaluates to #t if v is #f, otherwise it evaluates to #f. (SICP, p. 19) So, (not “cookies”) evaluates to #f 25 January 2002 CS 200 Spring 2002 6 2h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression1. Otherwise, the value of the if expression is the value of Expression2. Evaluate (not "cookies") => #f So, value of if is value of Expression2 => “starve” 25 January 2002 CS 200 Spring 2002 7 closer-color? (define (closer-color? sample color1 color2) (< (+ (abs (- (get-red color1) (get-red sample))) (abs (- (get-blue color1) (get-blue sample))) (abs (- (get-green color1) (get-green sample)))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) )) 25 January 2002 CS 200 Spring 2002 8 (+ (abs (- (get-red color1) (get-red sample))) (abs (- (get-blue color1) (get-blue sample))) (abs (- (get-green color1) (get-green sample)))) (define (closer-color? sample color1 color2) (< (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) )) 25 January 2002 CS 200 Spring 2002 9 (lambda ( ) (+ (abs (- (get-red color1 ) (get-red sample ))) (abs (- (get-blue color1) (get-blue sample ))) (abs (- (get-green color1) (get-green sample)))) (define (closer-color? sample color1 color2) (< (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) )) 25 January 2002 CS 200 Spring 2002 10 (define color-difference (lambda (colora colorb) (+ (abs (- (get-red colora ) (get-red colorb ))) (abs (- (get-blue colora) (get-blue colorb ))) (abs (- (get-green colora) (get-green colorb )))) )) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (+ (color-difference (abs (- (get-redcolor2 color2) (get-red sample))) sample) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) )) 25 January 2002 CS 200 Spring 2002 11 (define color-difference (lambda (colora colorb) (+ (abs (- (get-red colora) (get-red colorb))) (abs (- (get-green colora) (get-green colorb))) (abs (- (get-blue colora) (get-blue colorb)))))) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (color-difference color2 sample))) What is you want to use square instead of abs? 25 January 2002 CS 200 Spring 2002 12 (define color-difference (lambda (colora colorb) (+ (abs (- (get-red colora) (get-red colorb))) (abs (- (get-green colora) (get-green colorb))) (abs (- (get-blue colora) (get-blue colorb)))))) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (color-difference color2 sample))) 25 January 2002 CS 200 Spring 2002 13 (define color-difference (lambda (cf) (lambda (colora colorb) (+ (cf (- (get-red colora) (get-red colorb))) (cf (- (get-green colora) (get-green colorb))) (cf (- (get-blue colora) (get-blue colorb))))))) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (color-difference color2 sample))) 25 January 2002 CS 200 Spring 2002 14 (define color-difference (lambda (cf) (lambda (colora colorb) (+ (cf (- (get-red colora) (get-red colorb)) (cf (- (get-green colora) (get-green colorb)) (cf (- (get-blue colora) (get-blue colorb)))))))) (define (closer-color? sample color1 color2) (< ((color-difference square) color1 sample) ((color-difference square) color2 sample))) 25 January 2002 CS 200 Spring 2002 15 Questions on PS1 or PS2? Next: Fibonacci 25 January 2002 CS 200 Spring 2002 16 Fibonacci’s Problem Filius Bonacci, 1202 in Pisa: Suppose a newly-born pair of rabbits, one male, one female, are put in a field. Rabbits mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits. Suppose that our rabbits never die and that the female always produces one new pair (one male, one female) every month from the second month on. How many pairs will there be in one year? 25 January 2002 CS 200 Spring 2002 17 Rabbits From http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html 25 January 2002 CS 200 Spring 2002 18 Fibonacci Numbers GEB p. 136: These numbers are best defined recursively by the pair of formulas FIBO (n) = FIBO (n – 1) + FIBO (n – 2) for n > 2 FIBO (1) = FIBO (2) = 1 Can we turn this into a Scheme function? Note: SICP defines Fib with Fib(0)= 0 and Fib(1) = 1 for base case. Same function except for Fib(0) is undefined in GEB version. 25 January 2002 CS 200 Spring 2002 19 From last lecture: Defining Recursive Procedures 1. Be optimistic. – Assume you can solve it. – If you could, how would you solve a bigger problem. 2. Think of the simplest version of the problem, something you can already solve. (This is the base case.) 3. Combine them to solve the problem. 25 January 2002 CS 200 Spring 2002 20 Defining FIBO 1. Be optimistic - assume you can solve it, if you could, how would you solve a bigger problem. 2. Think of the simplest version of the problem, something you can already solve. 3. Combine them to solve the problem. 25 January 2002 These numbers are best defined recursively by the pair of formulas FIBO (n) = FIBO (n – 1) + FIBO (n – 2) for n > 2 FIBO (1) = FIBO (2) = 1 CS 200 Spring 2002 21 Defining fibo ;;; (fibo n) evaluates to the nth Fibonacci ;;; number (define (fibo n) FIBO (1) = FIBO (2) = 1 (if (or (= n 1) (= n 2)) 1 ;;; base case FIBO (n) = FIBO (n – 1) (+ (fibo (- n 1)) + FIBO (n – 2) (fibo (- n 2))))) for n > 2 25 January 2002 CS 200 Spring 2002 22 Fibo Results > (fibo 2) 1 > (fibo 3) Why can’t our 2 100,000x Apollo > (fibo 4) Guidance 3 Computer calculate > (fibo 10) (fibo 100)? 55 > (fibo 100) Still working after 4 hours… 25 January 2002 CS 200 Spring 2002 23 Tracing Fibo > (require-library "trace.ss") > (trace fibo) (fibo) > (fibo 3) |(fibo 3) | (fibo 2) | 1 | (fibo 1) | 1 |2 2 25 January 2002 CS 200 Spring 2002 This turns tracing on 24 > (fibo 5) |(fibo 5) | (fibo 4) | |(fibo 3) | | (fibo 2) ||1 | | (fibo 1) ||1 | |2 | |(fibo 2) | |1 |3 | (fibo 3) | |(fibo 2) | |1 | |(fibo 1) | |1 |2 |5 5 25 January 2002 (fibo 5) = (fibo 4) (fibo 3) + (fibo 2) (fibo 2) + (fibo 1) + 1 1 + 1 2 + 1 3 = 5 + (fibo 3) + (fibo 2) + (fibo 1) + 1 + 1 + 2 + 2 To calculate (fibo 5) we caluculated: (fibo 4) 1 time (fibo 3) 2 times (fibo 2) 3 times (fibo 1) 2 times = 8 calls to fibo = (fibo 6) How many calls to calculate (fibo 100)? CS 200 Spring 2002 25 fast-fibo (define (fast-fibo n) (define (fibo-worker a b count) (if (= count 1) b (fibo-worker (+ a b) a (- count 1)))) (fibo-worker 1 1 n)) 25 January 2002 CS 200 Spring 2002 26 Fast-Fibo Results > (fast-fibo 1) 1 > (fast-fibo 10) 55 > (time (fast-fibo 100)) cpu time: 0 real time: 0 gc time: 0 354224848179261915075 25 January 2002 CS 200 Spring 2002 27 ;;; The Earth's mass is 6.0 x 10^24 kg > (define mass-of-earth (* 6 (expt 10 24))) ;;; A typical rabbit's mass is 2.5 kilograms > (define mass-of-rabbit 2.5) > (/ (* mass-of-rabbit (fast-fibo 100)) mass-of-earth) 0.00014759368674135913 > (/ (* mass-of-rabbit (fast-fibo 110)) mass-of-earth) 0.018152823441189517 > (/ (* mass-of-rabbit (fast-fibo 119)) mass-of-earth) 1.379853393132076 > (exact->inexact (/ 119 12)) 9.916666666666666 According to Fibonacci’s model, after less than 10 years, rabbits would out-weigh the Earth! Beware the Bunnies!! 25 January 2002 CS 200 Spring 2002 28 Charge • PS1 Selected Answers on web – Read through the comments on your assignments and check the answers on the web • Beware the Bunnies! • Next time: – GEB Chapter 5 – More Higher Order Procedures 25 January 2002 CS 200 Spring 2002 29