David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions
24 January 2003CS 200 Spring Menu Defining Recursive Procedures Problem Set 1 Problem Set 2
24 January 2003CS 200 Spring 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.
24 January 2003CS 200 Spring 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 )) 201 > (find-closest 12 (list )) 11 > (find-closest 12 (list 95)) 95
24 January 2003CS 200 Spring 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?
24 January 2003CS 200 Spring 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.
24 January 2003CS 200 Spring 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))))
24 January 2003CS 200 Spring 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.
24 January 2003CS 200 Spring (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 The Base Case
24 January 2003CS 200 Spring Testing > (find-closest-number 200 (list )) 201 > (find-closest-number 0 (list 1)) 1 > (find-closest-number 0 (list )) first: expects argument of type ; given () (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))))
24 January 2003CS 200 Spring 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))))
24 January 2003CS 200 Spring 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?
24 January 2003CS 200 Spring Rabbits From
24 January 2003CS 200 Spring 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.
24 January 2003CS 200 Spring 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. Slide 3 Returns…
24 January 2003CS 200 Spring 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. 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
24 January 2003CS 200 Spring 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
24 January 2003CS 200 Spring 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)
24 January 2003CS 200 Spring Problem Set 1
24 January 2003CS 200 Spring Question 2 Without Evaluation Rules, Question 2 was “guesswork” Now you know the Evaluation Rules, you can answer Question 2 without any guessing!
24 January 2003CS 200 Spring d ( ) Evaluation Rule 3. Application. a. Evaluate all the subexpressions 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!
24 January 2003CS 200 Spring h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression 0. If it evaluates to #f, the value of the if expression is the value of Expression 2. Otherwise, the value of the if expression is the value of Expression 1. Evaluate (not "cookies")
24 January 2003CS 200 Spring Evaluate (not "cookies") Evaluation Rule 3. Application. a. Evaluate all the subexpressions “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
24 January 2003CS 200 Spring 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)
24 January 2003CS 200 Spring h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression 0. If it evaluates to #f, the value of the if expression is the value of Expression 1. Otherwise, the value of the if expression is the value of Expression 2. Evaluate (not "cookies") => #f So, value of if is value of Expression 2 => “starve”
24 January 2003CS 200 Spring Making Orange (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))) Ed Mitchell’s Answer (slightly adapted)
24 January 2003CS 200 Spring Making Orange (define orange (mix-color red yellow)) (show-color orange) (define reddish-orange (mix-color red orange)) (show-color reddish-orange)
24 January 2003CS 200 Spring brighter? (define (brightness color) (+ (get-red color) (get-green color) (get-blue color))) (define (brighter? color1 color2) (> (brightness color1) (brightness color2))) Chalermpong Worawannotai’s answer
24 January 2003CS 200 Spring (+ (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)))) (define (closer-color? sample color1 color2) (< )) closer-color?
24 January 2003CS 200 Spring (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (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))))
24 January 2003CS 200 Spring (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) color1 sample color1 sample (lambda ( )
24 January 2003CS 200 Spring (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) (color-difference color1 sample) colora colorb colora colorb (lambda (colora colorb) (define color-difference )) (color-difference color2 sample)
24 January 2003CS 200 Spring (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?
24 January 2003CS 200 Spring (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)))
24 January 2003CS 200 Spring (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)))
24 January 2003CS 200 Spring 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.)
24 January 2003CS 200 Spring 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: 1.new – must not be previously available (ancient Babylonians made mosaics) 2.useful 3.nonobvious about ¼ of the class came up with this method! (most of rest used abs instead, which works as well)
24 January 2003CS 200 Spring 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:
24 January 2003CS 200 Spring 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)
24 January 2003CS 200 Spring 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.)
24 January 2003CS 200 Spring Gosper Curves Gosper Curve Level 50 Gosper Curve Level 0 Gosper Curve Level 1
24 January 2003CS 200 Spring > (show-gosper smiley 0) > (show-gosper smiley 7) Curves are functions. We can transform them to make new curves.
24 January 2003CS 200 Spring 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!