Lecture 6 Recursing Recursively Richard Feynmans Van parked

  • Slides: 22
Download presentation
Lecture 6: Recursing Recursively Richard Feynman’s Van (parked outside theater where QED is playing)

Lecture 6: Recursing Recursively Richard Feynman’s Van (parked outside theater where QED is playing) Alan Alda playing Richard Feynman in QED CS 200: Computer Science David Evans University of Virginia 30 January 2002 CS 200 Spring 2002 http: //www. cs. virginia. edu/~evans Computer Science

Menu • Fix what I said about begin • GEB Chapter V –RTNs –Music

Menu • Fix what I said about begin • GEB Chapter V –RTNs –Music and Recursion 30 January 2002 CS 200 Spring 2002 2

Why begin is a special form • Eval 4 -begin. If the expression is

Why begin is a special form • Eval 4 -begin. If the expression is (begin Expression 0 Expression 1 … Expressionk) evaluate all the sub-expressions in order. The value of the begin expression is the value of Expressionk. Eval 3. If the expression is an application: a) Evaluate all the subexpressions of the combination (in any order) b) Apply the value of the first subexpression to the values of all the other subexpressions. 30 January 2002 CS 200 Spring 2002 3

Can we define begin? (define my-begin (lambda (expr 0 expr 1 expr 2)) >

Can we define begin? (define my-begin (lambda (expr 0 expr 1 expr 2)) > (begin (+ 3 4) 17 200) 200 > (my-begin (+ 3 4) 17 200) 200 > (begin (printf "hello ") (printf "world!") 200) hello world! 200 > (my-begin (printf "hello ") (printf "world!") 200) hello world! But, some other Scheme interpreter could produce 200 world!hello 30 January 2002 and still be CSfollowing 200 Spring the 2002 evaluation rules correctly! 4

Functions and Procedures What is a function? Mathematical entity that takes some inputs and

Functions and Procedures What is a function? Mathematical entity that takes some inputs and produces some outputs. A function always produces the same outputs from the same inputs. > (+ 2 2) 4 30 January 2002 CS 200 Spring 2002 5

Functions and Procedures What is a procedure? Description of a computation – how to

Functions and Procedures What is a procedure? Description of a computation – how to do something. Some procedures describe functions: they always produce the same outputs given the same inputs, and they don’t do anything else (noticable). Other procedures do not describe functions! 30 January 2002 CS 200 Spring 2002 6

Functions and Begin If all Scheme procedures were functions, would be need the begin

Functions and Begin If all Scheme procedures were functions, would be need the begin special form? 30 January 2002 CS 200 Spring 2002 7

GEB Chapter V You could spend the rest of your life just studying things

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 Quantum Electrodynamics DNA Sameness-in-differentness Game-playing algorithms 30 January 2002 CS 200 Spring 2002 8

Recursive Acronyms GOD : : = GOD Over Djinn Over Djinn … GNU :

Recursive Acronyms GOD : : = GOD Over Djinn Over Djinn … GNU : : = GNU’s Not Unix! = (GNU’s Not Unix!)’s Not Unix … 30 January 2002 CS 200 Spring 2002 9

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end Can we describe this

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end Can we describe this using Backus Naur Form? 30 January 2002 CS 200 Spring 2002 10

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : :

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : : = NOUN 30 January 2002 CS 200 Spring 2002 11

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : :

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : : = NOUN ORNATE NOUN : : = ARTICLE ADJECTIVE NOUN 30 January 2002 CS 200 Spring 2002 12

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : :

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : : = ARTICLE ADJECTIVE NOUN ORNATE NOUN : : = ARTICLE ADJECTIVE ADJECTIVE ADJECTIVE NOUN 30 January 2002 CS 200 Spring 2002 13

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : :

Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN : : = ARTICLE ADJECTIVES NOUN ADJECTIVES : : = ADJECTIVES : : = 30 January 2002 CS 200 Spring 2002 14

Recursive Transition Networks ORNATE NOUN ARTICLE begin ADJECTIVE NOUN end ORNATE NOUN : :

Recursive Transition Networks ORNATE NOUN ARTICLE begin ADJECTIVE NOUN end ORNATE NOUN : : = OPTARTICLE ADJECTIVES NOUN ADJECTIVES : : = ADJECTIVES : : = OPTARTICLE : : = ARTICLE OPTARTICLE : : = 30 January 2002 CS 200 Spring 2002 15

Recursive Transition Networks ORNATE NOUN ARTICLE begin ADJECTIVE NOUN end ORNATE NOUN : :

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? 30 January 2002 CS 200 Spring 2002 16

Music Harmony Kleines Harmonisches Labyrinth (Little Harmonic Labyrinth) 30 January 2002 CS 200 Spring

Music Harmony Kleines Harmonisches Labyrinth (Little Harmonic Labyrinth) 30 January 2002 CS 200 Spring 2002 17

Hey Jude John Lennon and Paul Mc. Cartney, 1968 30 January 2002 CS 200

Hey Jude John Lennon and Paul Mc. Cartney, 1968 30 January 2002 CS 200 Spring 2002 18

Hey Jude V: C = 3/2 * F Fif th sh Pu th ur

Hey Jude V: C = 3/2 * F Fif th sh Pu th ur Fo sh Pu Pu p Po sh p Tonic: F = 1 Tonic: F Pop Po Fif th IV: Bb = 4/3 * F 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. 30 January 2002 CS 200 Spring 2002 19

V: C = 3/2 * F fth Fi sh Pu Pu sh Pu Fo

V: C = 3/2 * F fth Fi sh Pu Pu sh Pu Fo ur th Fif sh Pop p p Po Verse : : = Po th IV: Bb = 4/3 * F Tonic: F = 1 V+V: Gm = 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 Fo Pu sh Pain, Hey Jude re- p Po ur th Bridge : : = Tonic: F = 1 And Anytime you feel the Tonic: F ders. Hey. Jude : : = Verse Bridge Dadada (ends on C) Verse Bridge Dadada Verse Better F Eb Bb F (Tonic) 30 January 2002 CS 200 Spring 2002 20

Music • Almost All Music Is Like This – Keeps coming back to what

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) 30 January 2002 CS 200 Spring 2002 21

PS 2 Lab hours tonight and Thursday 69 pm in Small Hall We might

PS 2 Lab hours tonight and Thursday 69 pm in Small Hall We might have lab hours Sunday, but only if there are people working Weds and Thurs who 30 January 2002 don’t finish. http: //www. fractalwisdom. com/Fractal. Wisdom/fractal. html CS 200 Spring 2002 22