430 likes | 445 Views
Learn how to define and solve problems recursively in computer science using base cases and combination strategies. Explore examples like finding the closest number in a list and solving Fibonacci's problem.
E N D
Lecture 4: Recursive Definitions David Evans http://www.cs.virginia.edu/evans CS200: Computer Science University of Virginia Computer Science
Menu • Defining Recursive Procedures • Problem Set 1 • Problem Set 2 CS 200 Spring 2003
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 2003
Example Define (find-closest goal numbers) that evaluates to the number in the list numbers list that is closest to goal: > (find-closest 200 (list 101 110 120 201 340 588)) 201 > (find-closest 12 (list 1 11 21)) 11 > (find-closest 12 (list 95)) 95 CS 200 Spring 2003
Find Closest Number Be optimistic! Assume you can define: (find-closest-number goal numbers) that finds the closest number to goal from the list of numbers. What if there is one more number? Can you write a function that finds the closest number to match from new-number and numbers? CS 200 Spring 2003
Find Best Match Strategy: If the new number is better, than the best match with the other number, use the new number. Otherwise, use the best match of the other numbers. CS 200 Spring 2003
Optimistic Function • (define (find-closest goal numbers) • (if (< (abs (- goal (first numbers))) • (abs (- goal • (find-closest goal (rest numbers))))) • (first numbers) • (find-closest goal (rest numbers)))) CS 200 Spring 2003
Defining Recursive Procedures 2. Think of the simplest version of the problem, something you can already solve. If there is only one number, that is the best match. CS 200 Spring 2003
The Base Case • (define (find-closest goal numbers) • (if (= 1 (length numbers)) • (first numbers) • (if (< (abs (- goal (first numbers))) • (abs (- goal • (find-closest goal (rest numbers))))) • (first numbers) • (find-closest goal (rest numbers)))) Same as before CS 200 Spring 2003
(define (find-closest goal numbers) • (if (= 1 (length numbers)) • (first numbers) • (if (< (abs (- goal (first numbers))) • (abs (- goal • (find-closest goal (rest numbers))))) • (first numbers) • (find-closest goal (rest numbers)))) Testing > (find-closest-number 200 (list 101 110 120 201 340 588)) 201 > (find-closest-number 0 (list 1)) 1 > (find-closest-number 0 (list )) first: expects argument of type <non-empty list>; given () CS 200 Spring 2003
Seen Anything Like This? (define (find-best-match sample tiles color-comparator) (if (null? tiles) ;;; If there are no tiles, (error "No tiles to match!") ;;; we must lose. (if (= (length tiles) 1) ;;; If there is just one tile, (first tiles) ;;; that tile is the best match. (pick-better-match ;;; Otherwise, the best match is sample ;;; either the first tile in tiles, (first tiles) ;;; or the best match we would find (find-best-match ;;; from looking at the rest of the sample ;;; tiles. Use pick-better-match (rest tiles) ;;; to determine which one is better. color-comparator) color-comparator)))) CS 200 Spring 2003
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 2003
Rabbits From http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html CS 200 Spring 2003
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 2003
Defining Recursive Procedures Slide 3 Returns… • 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 2003
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 2003
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 2003
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)? To be continued Monday (answer is in SICP, 1.2) CS 200 Spring 2003
Problem Set 1 CS 200 Spring 2003
Question 2 • Without Evaluation Rules, Question 2 was “guesswork” • Now you know the Evaluation Rules, you can answer Question 2 without any guessing! CS 200 Spring 2003
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 2003
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 Expression2. Otherwise, the value of the if expression is the value of Expression1. Evaluate (not "cookies") CS 200 Spring 2003
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 2003
Defining not (not v) evaluates to #t if v is #f, otherwise it evaluates to #f. (SICP, p. 19) (define (not v) (if v #f #t) CS 200 Spring 2003
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 2003
Making Orange Ed Mitchell’s Answer (slightly adapted) (define (mix-one f a b) (/ (+ (f a) (f b)) 2)) (define (mix-color color1 color2) (make-color (mix-one get-red color1 color2) (mix-one get-green color1 color2) (mix-one get-blue color1 color2))) CS 200 Spring 2003
Making Orange (define orange (mix-color red yellow)) (show-color orange) (define reddish-orange (mix-color red orange)) (show-color reddish-orange) CS 200 Spring 2003
brighter? Chalermpong Worawannotai’s answer (define (brightness color) (+ (get-red color) (get-green color) (get-blue color))) (define (brighter? color1 color2) (> (brightness color1) (brightness color2))) CS 200 Spring 2003
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 2003
(+ (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 2003
(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 2003
(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 2003
(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 if you want to use square instead of abs? CS 200 Spring 2003
(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 2003
(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 2003
The Patented RGB RMS Method /* This is a variation of RGB RMS error. The final square-root has been eliminated to */ /* speed up the process. We can do this because we only care about relative error. */ /* HSV RMS error or other matching systems could be used here, as long as the goal of */ /* finding source images that are visually similar to the portion of the target image */ /* under consideration is met. */ for(i = 0; i > size; i++) { rt = (int) ((unsigned char)rmas[i] - (unsigned char)image->r[i]); gt = (int) ((unsigned char)gmas[i] - (unsigned char) image->g[i]; bt = (int) ((unsigned char)bmas[i] - (unsigned char)image->b[i]; result += (rt*rt+gt*gt+bt*bt); } Your code should never look like this! Use new lines and indenting to make it easy to understand the structure of your code! (Note: unless you are writing a patent. Then the goal is to make it as hard to understand as possible.) CS 200 Spring 2003
The Patented RGB RMS Method rt = rmas[i] - image->r[i]; gt = gmas[i] - image->g[i]; bt = bmas[i] - image->b[i]; result += (rt*rt + gt*gt + bt*bt); • Patent requirements: • new – must not be previously available • (ancient Babylonians made mosaics) • useful • nonobvious • about ¼ of the class came up with this method! • (most of rest used abs instead, which works as well) CS 200 Spring 2003
PS1 Grading Scale • Gold Star – Excellent Work. You got everything I wanted on this PS. • Green Star – Good Work. You got most things on this PS, but some answers could be better. • Silver Star – Some problems. Make sure you understand the solutions on today’s slides. PS1 Average: CS 200 Spring 2003
No upper limit - Double Gold Star: exceptional work! Better than I expected anyone would do. - Triple Gold Star: Better than I thought possible (deserving of a patent!) - Quadruple Gold Star: You have broken important new ground in CS which should be published in a major journal - Quintuple Gold Star: You deserve to win a Turing Award! (e.g., a fast, general way to make the best non-repeating photomosaic) CS 200 Spring 2003
Problem Set 2 • Unlike PS1, you should be able to understand all the provided code (except, don’t worry about the trigonometry) • Main ideas: recursion, procedures • We have covered everything you need after today • As we progress, problem sets will expect you to write more code on your own. • PS8 is “Do something worthwhile using things you have learned from this class.” (But will be a little bit more specific about what is expected.) CS 200 Spring 2003
Gosper Curves Gosper Curve Level 0 Gosper Curve Level 1 Gosper Curve Level 50 CS 200 Spring 2003
> (show-gosper smiley 0) > (show-gosper smiley 7) Curves are functions. We can transform them to make new curves. CS 200 Spring 2003
Charge • Many, many important problems can be solved by recursive definitions • Read GEB Chapter 5 before Monday’s class: lots of great stuff in there! CS 200 Spring 2003