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

Slides:



Advertisements
Similar presentations
Biography ( ) Fibonacci is a short for the Latin "filius Bonacci" which means "the son of Bonacci" but his full name was Leonardo of Pisa, or Leonardo.
Advertisements

Cs1120 Fall 2009 David Evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynmans van Alan Alda playing.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 6: Cons car cdr sdr wdr.
Introduction to Recursion and Recursive Algorithms
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!.
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.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Fibonacci Numbers.
Strong Induction 2/27/121. Induction Rule 2/27/122.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
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.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
THE BEATLES By:Madison.
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.
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
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.
Dr Zhang Fall 2014 Fordham University
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 CS150: Computer Science University of Virginia Computer Science Lecture 25: Gödel and Computability Halting.
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.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
Waiters’ Nightmare A single rectangular table can seat 6 hungry humans.
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.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
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 8: Recursing Recursively Richard Feynman’s.
Computer Science We use computers, but the focus is on applied problem solving. One of my favorite videos talks about computer sciencecomputer science.
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.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
“Toy” Model: Fibonacci The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci.
Recursion.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Hassan Khosravi / Geoffrey Tien
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Recursive functions.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
Class 34: Models of Computation CS200: Computer Science
Lecture 3: Rules of Evaluation CS200: 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 5: Recursing Recursively Richard Feynman’s Van (parked outside the theater where QED is playing) Alan Alda playing Richard Feynman in QED Now playing: JS Bach, The Art of Fugue

26 January 2004CS 200 Spring Menu Recursive Procedures GEB Chapter V –Fibonacci Returns –RTNs –Music and Recursion

26 January 2004CS 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.

26 January 2004CS 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

26 January 2004CS 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?

26 January 2004CS 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.

26 January 2004CS 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))))

26 January 2004CS 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.

26 January 2004CS 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

26 January 2004CS 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))))

26 January 2004CS 200 Spring Seen Anything Like This? (define (find-best-match sample tiles color-comparator) (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))))

26 January 2004CS 200 Spring 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) –Quantum Electrodynamics (PS7) –DNA (next to last lecture) –Sameness-in-differentness –Game-playing algorithms

26 January 2004CS 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?

26 January 2004CS 200 Spring Rabbits From

26 January 2004CS 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.

26 January 2004CS 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…

26 January 2004CS 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

26 January 2004CS 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

26 January 2004CS 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 1Mx Apollo Guidance Computer calculate (fibo 100) ? To be continued Wednesday (answer is in SICP, 1.2)

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN Can we describe this using Backus Naur Form?

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= NOUN

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= NOUN ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN

26 January 2004CS 200 Spring 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

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::=

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::= OPTARTICLE ::= ARTICLE OPTARTICLE ::=

26 January 2004CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= [ ARTICLE ] ADJECTIVE* NOUN Using extended BNF notation: [ item ]item is optional (0 or 1 of them) item*0 or more items Which notation is better?

26 January 2004CS 200 Spring Music Harmony Kleines Harmonisches Labyrinth (Little Harmonic Labyrinth)

26 January 2004CS 200 Spring Hey Jude John Lennon and Paul McCartney, 1968

26 January 2004CS 200 Spring 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.

26 January 2004CS 200 Spring 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

26 January 2004CS 200 Spring 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)

26 January 2004CS 200 Spring Charge Challenge: Try to find a “pop” song with a 3-level deep harmonic stack PS2: due 1 week from today Be optimistic! You know everything you need to finish it now, so get started!