Transcript Slides

Now playing: JS Bach, The Art of Fugue
Lecture 5:
Recursing Recursively
Richard Feynman’s Van
(parked outside the theater
where QED is playing)
Alan Alda playing Richard Feynman in QED
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/~evans
Menu
• Fibonacci Returns
• GEB Chapter V
–RTNs
–Music and Recursion
27 January 2003
CS 200 Spring 2003
2
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 2003
CS 200 Spring 2003
3
Defining fibo
;;; (fibo n) evaluates to the nth Fibonacci
;;; number
(define (fibo n)
FIBO (1) = FIBO (2) = 1
(if (or (= n 1) (= n 2))
1 ;;; base case
FIBO (n) =
FIBO (n – 1)
(+ (fibo (- n 1))
+ FIBO (n – 2)
(fibo (- n 2)))))
for n > 2
27 January 2003
CS 200 Spring 2003
4
Fibo Results
> (fibo 2)
1
Why
can’t
our
> (fibo 3)
100,000x Apollo
2
Guidance
> (fibo 4)
3
Computer calculate
> (fibo 10)
(fibo 100)?
55
> (fibo 100)
Still working after 4 hours…
27 January 2003
CS 200 Spring 2003
5
Tracing Fibo
> (require-library "trace.ss")
> (trace fibo)
(fibo)
> (fibo 3)
|(fibo 3)
|
(fibo 2)
|
1
|
(fibo 1)
|
1
|2
2
27 January 2003
CS 200 Spring 2003
This turns tracing on
6
> (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
27 January 2003
(fibo 5) = (fibo 4)
(fibo 3) +
(fibo 2)
(fibo 2) + (fibo 1) +
1
1
+ 1
2
+
1
3
= 5
+ (fibo 3)
+ (fibo 2) + (fibo 1)
+ 1
+ 1
+ 2
+ 2
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)?
CS 200 Spring 2003
7
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 2003
CS 200 Spring 2003
8
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
27 January 2003
CS 200 Spring 2003
9
;;; 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!
Beware the Bunnies!!
27 January 2003
CS 200 Spring 2003
10
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 2003
CS 200 Spring 2003
11
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
Can we describe this using Backus Naur Form?
27 January 2003
CS 200 Spring 2003
12
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= NOUN
27 January 2003
CS 200 Spring 2003
13
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= NOUN
ORNATE NOUN ::= ARTICLE ADJECTIVE NOUN
27 January 2003
CS 200 Spring 2003
14
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
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 2003
CS 200 Spring 2003
15
Recursive Transition Networks
ORNATE NOUN
begin
ARTICLE
ADJECTIVE
NOUN
end
ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN
ADJECTIVES
::= ADJECTIVE ADJECTIVES
ADJECTIVES
::=
27 January 2003
CS 200 Spring 2003
16
Recursive Transition Networks
ORNATE NOUN
ARTICLE
begin
ADJECTIVE
NOUN
end
ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN
ADJECTIVES
::= ADJECTIVE ADJECTIVES
ADJECTIVES
::=
OPTARTICLE
::= ARTICLE
OPTARTICLE
::=
27 January 2003
CS 200 Spring 2003
17
Recursive Transition Networks
ORNATE NOUN
ARTICLE
begin
ADJECTIVE
NOUN
end
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 2003
CS 200 Spring 2003
18
Music Harmony
Kleines Harmonisches Labyrinth
(Little Harmonic Labyrinth)
27 January 2003
CS 200 Spring 2003
19
Hey Jude
John Lennon and Paul McCartney, 1968
27 January 2003
CS 200 Spring 2003
20
Hey Jude
V: C = 3/2 * F
V: C = 3/2 * F
IV: Bb = 4/3 * F
Tonic: F
Tonic: F
Tonic: F = 1
Tonic: F
Tonic: Hey Jude, don’t make it
V: bad. take a sad song and make it
Tonic: better ReIV: member to let her into your
Tonic: heart, then you can
V: start to make it betTonic: -ter.
27 January 2003
CS 200 Spring 2003
21
V: C = 3/2 * F
V: C = 3/2 * F
IV: Bb = 4/3 * F
Verse ::=
Tonic: F
Tonic: F
Tonic: F = 1
V+V: Gm = 3/2 * 3/2 * F
Tonic: F
-frain, don’t’ carry the
V: C = 3/2 * F
world up-on you shoul-
IV: Bb = 4/3 * F
Bridge ::=
Pain, Hey Jude re-
Tonic: F = 1
And Anytime you feel the
Tonic: F
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 2003
CS 200 Spring 2003
22
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 2003
CS 200 Spring 2003
23
Charge
• Challenge: Try
to find a song
with a 3-level
deep harmonic
stack
• PS2: Lab
hours tonight
(7-8:30) and
Thursday (89:30pm) in
Small Hall
http://www.fractalwisdom.com/FractalWisdom/fractal.html
27 January 2003
CS 200 Spring 2003
24