David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynmans van Alan Alda playing.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
Introduction to Recursion and Recursive Algorithms
We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.
David Evans CS200: Computer Science University of Virginia Computer Science Class 30: Models of Computation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
Class 5: Constructing Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Cs3102: Theory of Computation Class 24: NP-Completeness Spring 2010 University of Virginia David Evans.
Aim: Arithmetic Sequence Course: Alg. 2 & Trig. Do Now: Aim: What is an arithmetic sequence and series? Find the next three numbers in the sequence 1,
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
Class 7: List Procedures David Evans cs1120 Fall 2009.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 8: Recursing Recursively Richard Feynman’s.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
David Evans CS200: Computer Science University of Virginia Computer Science Class 26: Halting Problem It is plain at any.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
Lesson #6 Modular Programming and Functions.
Class 8: Procedure Practice
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lesson #6 Modular Programming and Functions.
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CS 326 Programming Languages, Concepts and Implementation
Lecture 6: Programming with Data CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Lecture 11: All Sorts CS200: Computer Science University of Virginia
Functional Programming
Lecture 22: Gödel’s Theorem CS200: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 9: The Great Lambda Tree of Knowledge and Power
Class 24: Computability Halting Problems Hockey Team Logo
Class 34: Models of Computation CS200: Computer Science
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture 3: Rules of Evaluation CS200: Computer Science
Class 26: Modeling Computing CS150: Computer Science
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Presentation transcript:

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!