David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.

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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
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.
Original Question: How fast rabbits can rabbits breed in ideal circumstances? Suppose a newly-born pair of.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
Guess who!.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
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.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
Hey Julie Made by Dale Wang. Whole Lyrics(1) Hey Jude, don't make it bad Take a sad song and make it better Remember to let her into your heart Then you.
David Evans Turing Machines, Busy Beavers, and Big Questions about Computing.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
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 12: Something about Sneezewort From.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
Dr Zhang Fall 2014 Fordham University
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 Class 16: Mutation M. C. Escher, Day and Night.
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.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
Do you have your nametags today? Ready for class!.
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 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.
“Toy” Model: Fibonacci The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
CS 326 Programming Languages, Concepts and Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
The Metacircular Evaluator
Lecture 11: All Sorts CS200: Computer Science University of Virginia
Lecture 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
Class 34: Models of Computation CS200: Computer Science
6.001 SICP Interpretation Parts of an interpreter
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s Van (parked outside the theater where QED is playing) Alan Alda playing Richard Feynman in QED

2 Lecture 9: Recursing Recursively Menu find-closest-number GEB Chapter V

3 Lecture 9: Recursing Recursively Example Define a procedure find-closest-number that takes two inputs, a goal number, and a list of numbers, and produces the number in the list numbers list that is closest to goal: > (find-closest-number 150 (list )) 157 > (find-closest-number 12 (list )) 11 > (find-closest-number 12 (list 95)) 95

4 Lecture 9: Recursing Recursively 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?

5 Lecture 9: Recursing Recursively Finding the Closest Strategy: If the first number is closer than the closest number of the rest of the numbers, use the first number. Otherwise, use the closet number of the rest of the numbers.

6 Lecture 9: Recursing Recursively Optimistic Function (define (find-closest goal numbers) (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (car numbers) (find-closest-number goal (cdr numbers))))

7 Lecture 9: Recursing Recursively 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.

8 Lecture 9: Recursing Recursively (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (car numbers) (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (car numbers) (find-closest-number goal (cdr numbers)))) Same as before The Base Case

9 Lecture 9: Recursing Recursively Testing > (find-closest-number 150 (list )) 157 > (find-closest-number 0 (list 1)) 1 > (find-closest-number 0 (list )) first: expects argument of type ; given () (define (find-closest-number goal numbers) (if (= 1 (length numbers)) (car numbers) (if (< (abs (- goal (car numbers))) (abs (- goal (find-closest-number goal (cdr numbers))))) (car numbers) (find-closest-number goal (cdr numbers))))

10 Lecture 9: Recursing Recursively Generalizing find-closest-number How would we implement find-closest-number-without-going-over? What about find-closest-word?... The “closeness” metric should be a procedure parameter

11 Lecture 9: Recursing Recursively find-closest (define (find-closest goal lst closeness) (if (= 1 (length lst)) (car lst) (if (< (closeness goal (car lst)) (closeness goal (find-closest goal (cdr lst) closeness))) (car lst) (find-closest goal (cdr lst) closeness))) How can we implement find-closest number with find-closest?

12 Lecture 9: Recursing Recursively find-closest-number (define (find-closest-number goal numbers) (find-closest goal numbers (lambda (a b) (abs (- a b))))) (define (find-closest-below goal numbers) (find-closest goal numbers (lambda (a b) (if (>= a b) (- a b) 99999))))

13 Lecture 9: Recursing Recursively find-closest (define (find-closest goal lst closeness) (if (= 1 (length lst)) (car lst) (if (< (closeness goal (car lst)) (closeness goal (find-closest goal (cdr lst) closeness))) (car lst) (find-closest goal (cdr lst) closeness))) How can we avoid needing to evaluate find-closest twice?

14 Lecture 9: Recursing Recursively find-closest (define (find-closest goal lst closeness) (if (= 1 (length lst)) (car lst) (pick-closest closeness goal (car lst) (find-closest goal (cdr lst) closeness)))) (define (pick-closest closeness goal num1 num2) (if (< (closeness goal num1) (closeness goal num2)) num1 num2))

15 Lecture 9: Recursing Recursively Seen Anything Like This? (define (find-best-match sample tiles color-comparator) (if (= (length tiles) 1) (car tiles) (pick-better-match sample (car tiles) (find-best-match sample (cdr tiles) color-comparator) color-comparator)))) (define (pick-better-match sample tile1 tile2 color-comparator) (if (color-comparator sample (tile-color tile1) (tile-color tile2)) tile1 tile2))

16 Lecture 9: Recursing Recursively GEB Chapter V You could spend the rest of your life just studying things in this chapter (25 pages)! –Music Harmony –Stacks and Recursion –Theology –Language Structure –Number Sequences –Chaos –Fractals (PS3 out today) –Quantum Electrodynamics (later lecture) –DNA (later lecture) –Sameness-in-differentness –Game-playing algorithms (later lecture)

17 Lecture 9: Recursing Recursively 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?

18 Lecture 9: Recursing Recursively Rabbits From

19 Lecture 9: Recursing Recursively 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 procedure?

20 Lecture 9: Recursing Recursively 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

21 Lecture 9: Recursing Recursively 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

22 Lecture 9: Recursing Recursively Fibo Results > (fibo 2) 1 > (fibo 3) 2 > (fibo 4) 3 > (fibo 10) 55 > (fibo 60) Still working after 4 hours… Why can’t our 4Mx Apollo Guidance Computer figure out how many rabbits there will be in 5 years? To be continued...

23 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN Can we describe this using Backus Naur Form?

24 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= NOUN

25 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN

26 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE ADJECTIVE NOUN

27 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::=

28 Lecture 9: Recursing Recursively Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::= ε OPTARTICLE ::= ARTICLE OPTARTICLE ::= ε Which notation is better?

29 Lecture 9: Recursing Recursively Music Harmony Kleines Harmonisches Labyrinth (Little Harmonic Labyrinth)

30 Lecture 9: Recursing Recursively Hey Jude John Lennon and Paul McCartney, 1968

31 Lecture 9: Recursing Recursively Hey Jude Tonic: F = 1 V: C = 3/2 * F Tonic: F IV: Bb = 4/3 * F Push Fifth Push Fourth Pop Tonic: F Pop V: C = 3/2 * F Tonic: F Push Fifth Pop Tonic: Hey Jude, don’t make it V: bad. take a sad song and make it Tonic: better Re- IV: member to let her into your Tonic: heart, then you can V: start to make it bet- Tonic: -ter.

32 Lecture 9: Recursing Recursively Tonic: F = 1 V: C = 3/2 * F Tonic: F IV: Bb = 4/3 * F Push Fifth Push Fourth Pop Tonic: F Pop V: C = 3/2 * F Tonic: F Push Fifth Pop Verse ::= Bridge ::= Tonic: F = 1 V+V: Gm = 3/2 * 3/2 * F Push Fourth V: C = 3/2 * F Tonic: F Pop IV: Bb = 4/3 * F And Anytime you feel the Pain, Hey Jude re- -frain, don’t’ carry the world up-on you shoul- ders. HeyJude ::= Verse VBBD VBBD Verse Verse Better Coda VBBD ::= Verse Bridge Bridge Dadada (ends on C) Coda ::= F Eb Bb F Coda

33 Lecture 9: Recursing Recursively Music Almost All Music Is Like This –Pushes and pops the listener’s stack, but doesn’t go too far away from it –Repeats similar patterns in structured way –Keeps coming back to Tonic, and Ends on the Tonic Any famous Beatles song that doesn’t end on Tonic? “A Day in the Life” (starts on G, ends on E)

34 Lecture 9: Recursing Recursively Charge Challenge: Try to find a “pop” song with a 3-level deep harmonic stack PS3: due in one week Be optimistic! You know everything you need to finish it now, and it is longer than ps2, so get started now!