Transcript slides
Lecture 6: 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 • 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 (begin Expression0 Expression1 … 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 (expr0 expr1 expr2) expr2)) > (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 2002evaluation rules correctly! 4 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 > (+ 2 2) 4 > (+ 2 2) 4 30 January 2002 CS 200 Spring 2002 5 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 special form? 30 January 2002 CS 200 Spring 2002 7 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 = GOD Over Djinn Over Djinn = GOD Over Djinn 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 using Backus Naur Form? 30 January 2002 CS 200 Spring 2002 10 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 ::= 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 ::= 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 30 January 2002 CS 200 Spring 2002 13 Recursive Transition Networks ORNATE NOUN begin ARTICLE ADJECTIVE NOUN end ORNATE NOUN ::= ARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE ADJECTIVES ADJECTIVES ::= 30 January 2002 CS 200 Spring 2002 14 Recursive Transition Networks ORNATE NOUN ARTICLE begin ADJECTIVE NOUN end ORNATE NOUN ::= OPTARTICLE ADJECTIVES NOUN ADJECTIVES ::= ADJECTIVE 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 ::= [ 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 2002 17 Hey Jude John Lennon and Paul McCartney, 1968 30 January 2002 CS 200 Spring 2002 18 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. 30 January 2002 CS 200 Spring 2002 19 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 Verse Bridge Bridge Dadada (ends on C) Verse Bridge Bridge Dadada Verse 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 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 PS2 Lab hours tonight and Thursday 6-9pm 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/FractalWisdom/fractal.html CS 200 Spring 2002 22