Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~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

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

3 27 January 2003CS 200 Spring 20033 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.

4 27 January 2003CS 200 Spring 20034 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

5 27 January 2003CS 200 Spring 20035 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) ?

6 27 January 2003CS 200 Spring 20036 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

7 27 January 2003CS 200 Spring 20037 > (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) + 1 + 1 + 1 1 + 1 2 + 1 + 2 3 + 2 = 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) ?

8 27 January 2003CS 200 Spring 20038 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))

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

10 27 January 2003CS 200 Spring 200310 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) 0.00014759368674135913 > (/ (* mass-of-rabbit (fast-fibo 110)) mass-of-earth) 0.018152823441189517 > (/ (* mass-of-rabbit (fast-fibo 119)) mass-of-earth) 1.379853393132076 > (exact->inexact (/ 119 12)) 9.916666666666666 According to Fibonacci’s model, after less than 10 years, rabbits would out-weigh the Earth!

11 27 January 2003CS 200 Spring 200311 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)

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

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

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

15 27 January 2003CS 200 Spring 200315 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

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

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

18 27 January 2003CS 200 Spring 200318 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?

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

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

21 27 January 2003CS 200 Spring 200321 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.

22 27 January 2003CS 200 Spring 200322 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

23 27 January 2003CS 200 Spring 200323 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)

24 27 January 2003CS 200 Spring 200324 http://www.fractalwisdom.com/FractalWisdom/fractal.html 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


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

Similar presentations


Ads by Google