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

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.
Chapter 1: Sequences and Sets 1.1 Sequences. Sequences What number comes next? 1, 2, 3, 4, 5, ____ 2, 6, 10, 14, 18, ____ 1, 2, 4, 8, 16, ____
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
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 CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
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.
“If a man does not keep pace with his companions, perhaps it is because he hears a different drummer. Let him step to the music which he hears, however.
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.
Class 19: Undecidability in Theory and Practice David Evans cs302: Theory of Computation University of Virginia Computer.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
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.
THE BEATLES By:Madison.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 12: Something about Sneezewort From.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
Cs3102: Theory of Computation Class 14: Turing Machines Spring 2010 University of Virginia David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
Unit 5 – Series, Sequences, and Limits Section 5.2 – Recursive Definitions Calculator Required.
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 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 23: Programming with Objects.
David Evans Class 16: NP-Completeness / The Story so Far CS150: Computer Science University of Virginia Computer Science.
The Beatles were the most famous pop and rock group of the 1960s. Their music was popular not only in Britain, but all over the world.
The Beatles were the most famous pop and rock group of the 1960s. Their music was popular not only in Britain, but all over the world.
Cs1120 Fall 2009 David Evans Lecture 18: Changing State Sounds of Colossus:
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 22: Objectifying Objects.
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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
Guess who!.
Recursion.
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 22: Inheritance CS150: Computer Science University of Virginia
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Introduction to Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Turing Machines, Busy Beavers, and Big Questions about Computing
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lecture 11: All Sorts CS200: Computer Science University of Virginia
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Lecture 24: Metalinguistics CS200: Computer Science
Class 34: Models of Computation CS200: Computer Science
Applications of Stacks and Queues
Lecture 3: Rules of Evaluation CS200: Computer Science
Class 26: Modeling Computing CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
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

27 January 2003CS 200 Spring Menu Fibonacci Returns GEB Chapter V –RTNs –Music and Recursion

27 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.

27 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

27 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) ?

27 January 2003CS 200 Spring Tracing Fibo > (require-library "trace.ss") > (trace fibo) (fibo) > (fibo 3) |(fibo 3) | (fibo 2) | 1 | (fibo 1) | 1 |2 2 This turns tracing on

27 January 2003CS 200 Spring > (fibo 5) |(fibo 5) | (fibo 4) | |(fibo 3) | | (fibo 2) | | 1 | | (fibo 1) | | 1 | |2 | |(fibo 2) | |1 | 3 | (fibo 3) | |(fibo 2) | |1 | |(fibo 1) | |1 | 2 |5 5 (fibo 5) = (fibo 4) + (fibo 3) (fibo 3) + (fibo 2) + (fibo 2) + (fibo 1) (fibo 2) + (fibo 1) = 5 To calculate (fibo 5) we caluculated: (fibo 4)1 time (fibo 3)2 times (fibo 2)3 times (fibo 1)2 times = 8 calls to fibo = (fibo 6) How many calls to calculate (fibo 100) ?

27 January 2003CS 200 Spring fast-fibo (define (fast-fibo n) (define (fibo-worker a b count) (if (= count 1) b (fibo-worker (+ a b) a (- count 1)))) (fibo-worker 1 1 n))

27 January 2003CS 200 Spring Fast-Fibo Results > (fast-fibo 1) 1 > (fast-fibo 10) 55 > (time (fast-fibo 100)) cpu time: 0 real time: 0 gc time:

27 January 2003CS 200 Spring Beware the Bunnies!! ;;; The Earth's mass is 6.0 x 10^24 kg > (define mass-of-earth (* 6 (expt 10 24))) ;;; A typical rabbit's mass is 2.5 kilograms > (define mass-of-rabbit 2.5) > (/ (* mass-of-rabbit (fast-fibo 100)) mass-of-earth) > (/ (* mass-of-rabbit (fast-fibo 110)) mass-of-earth) > (/ (* mass-of-rabbit (fast-fibo 119)) mass-of-earth) > (exact->inexact (/ )) According to Fibonacci’s model, after less than 10 years, rabbits would out-weigh the Earth!

27 January 2003CS 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 (PS2 and PS3) –Quantum Electrodynamics (PS7) –DNA (next to last lecture) –Sameness-in-differentness –Game-playing algorithms (CS201J)

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

27 January 2003CS 200 Spring Recursive Transition Networks ARTICLEADJECTIVE NOUN end begin ORNATE NOUN ORNATE NOUN ::= NOUN

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

27 January 2003CS 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

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

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

27 January 2003CS 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?

27 January 2003CS 200 Spring Music Harmony Kleines Harmonisches Labyrinth (Little Harmonic Labyrinth)

27 January 2003CS 200 Spring Hey Jude John Lennon and Paul McCartney, 1968

27 January 2003CS 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.

27 January 2003CS 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

27 January 2003CS 200 Spring Music Almost All Music Is Like This –Keeps coming back to what it started on (Tonic) –Doesn’t go too far away from it –Repeats similar patterns in structured way –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)

27 January 2003CS 200 Spring Charge Challenge: Try to find a song with a 3-level deep harmonic stack PS2: Lab hours tonight (7-8:30) and Thursday (8- 9:30pm) in Small Hall