1 / 29

Lecture 5: Recursion - Beware the Lizards!

This lecture discusses recursion in computer science, focusing on evaluation rules and examples. Topics include guessing, evaluating subexpressions, if expressions, and color comparisons.

jnoella
Download Presentation

Lecture 5: Recursion - Beware the Lizards!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5: Recursion Beware the Lizards! David Evans http://www.cs.virginia.edu/~evans CS200: Computer Science University of Virginia Computer Science

  2. Menu • PS1 Comments • FIBO Example CS 200 Spring 2002

  3. Problem Set 1 • Without Evaluation Rules, Question 2 was “guesswork” • Now you know the Evaluation Rules, Question 2 should be obvious CS 200 Spring 2002

  4. 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! CS 200 Spring 2002

  5. 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") CS 200 Spring 2002

  6. 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 CS 200 Spring 2002

  7. 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” CS 200 Spring 2002

  8. 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)))) )) CS 200 Spring 2002

  9. (+ (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)))) )) CS 200 Spring 2002

  10. (lambda ( ) (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) sample color1 color1 sample sample color1 (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)))) )) CS 200 Spring 2002

  11. (define color-difference (lambda (colora colorb) (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) colorb colora colora colorb colorb colora )) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (color-difference color2 sample) )) 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))) What is you want to use square instead of abs? CS 200 Spring 2002

  13. (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))) 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 color1 sample) (color-difference color2 sample))) CS 200 Spring 2002

  15. (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))) CS 200 Spring 2002

  16. Next: Fibonacci Questions on PS1 or PS2? CS 200 Spring 2002

  17. 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? CS 200 Spring 2002

  18. Rabbits From http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html CS 200 Spring 2002

  19. 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. CS 200 Spring 2002

  20. From last lecture: Defining Recursive Procedures • Be optimistic. • Assume you can solve it. • If you could, how would you solve a bigger problem. • Think of the simplest version of the problem, something you can already solve. (This is the base case.) • Combine them to solve the problem. CS 200 Spring 2002

  21. Defining FIBO • Be optimistic - assume you can solve it, if you could, how would you solve a bigger problem. • Think of the simplest version of the problem, something you can already solve. • Combine them to solve the problem. 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

  22. Defining fibo ;;; (fibo n) evaluates to the nth Fibonacci ;;; number (define (fibo n) (if (or (= n 1) (= n 2)) 1 ;;; base case (+ (fibo (- n 1)) (fibo (- n 2))))) FIBO (1) = FIBO (2) = 1 FIBO (n) = FIBO (n – 1) + FIBO (n – 2) for n > 2 CS 200 Spring 2002

  23. Fibo Results > (fibo 2) 1 > (fibo 3) 2 > (fibo 4) 3 > (fibo 10) 55 > (fibo 100) Still working after 4 hours… Why can’t our 100,000x Apollo Guidance Computer calculate (fibo 100)? CS 200 Spring 2002

  24. Tracing Fibo > (require-library "trace.ss") > (trace fibo) (fibo) > (fibo 3) |(fibo 3) | (fibo 2) | 1 | (fibo 1) | 1 |2 2 This turns tracing on CS 200 Spring 2002

  25. > (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 (fibo 5) = (fibo 4) + (fibo 3) (fibo 3) + (fibo 2) + (fibo 2) + (fibo 1) (fibo 2) + (fibo 1) + 1 + 1 + 1 1 + 1 2 + 1 + 2 3 + 2 = 5 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

  26. 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)) CS 200 Spring 2002

  27. 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 CS 200 Spring 2002

  28. ;;; 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!! CS 200 Spring 2002

  29. 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 CS 200 Spring 2002

More Related