David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.

Slides:



Advertisements
Similar presentations
Class 3: Rules of Evaluation David Evans cs1120 Fall 2009.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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 CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 5: Logs and Trees
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursing Recursively Richard Feynman’s.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Class 5: Constructing Procedures David Evans cs1120 Fall 2009.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
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 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
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 4: The Value of Everything.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 5: Recursing on Lists.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
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 4: Programming with Data.
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 Lecture 23: Intractable Problems (Smiley Puzzles.
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.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Lecture 6: Lambda Calculus
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Class 8: Procedure Practice
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Class 14: Intractable Problems CS150: Computer Science
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 22: Gödel’s Theorem CS200: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Class 24: Computability Halting Problems Hockey Team Logo
Class 31: Universal Turing Machines CS200: Computer Science
Class 34: Models of Computation CS200: Computer Science
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
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions

23 January 2004CS 200 Spring Menu Rules of Evaluation –PS1 Question 2 Defining Recursive Procedures

23 January 2004CS 200 Spring (square 4) Eval Rule 3a: Application square 4 Eval Rule 2: Names (lambda (x) (* x x)) Eval Rule 1: Primitive 4 Eval Rule 3b: Application Apply Rule 2: Compound Application (* 4 4 ) Eval Rule 3a: Application * 4 4 Eval Rule 1: Primitive 4 4 # Eval Rule 3b: Application Apply Rule 1: Primitive Application 16

23 January 2004CS 200 Spring Composition Define a procedure that takes two parameters: f a procedure that takes one parameter g a procedure that takes one parameter and evaluates to a procedure that is the composition of f and g. ((compose f g) x) should evaluate to the same thing as (f (g x)).

23 January 2004CS 200 Spring Evaluation > (define compose (lambda (f g) (lambda (x) (f (g x))))) > (define square (lambda (x) (* x x))) > (square 2) 4 > (compose square square) # > ((compose square square) 2) 16

23 January 2004CS 200 Spring Question 2 Without Evaluation Rules, Question 2 was “guesswork” Now you know the Evaluation Rules, you can answer Question 2 without any guessing!

23 January 2004CS 200 Spring d ( ) Evaluation Rule 3. Application. a. Evaluate all the subexpressions b. Apply the value of the first subexpression to the values of all the other subexpressions Error: 100 is not a procedure, we only have apply rules for procedures!

23 January 2004CS 200 Spring h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression 0. If it evaluates to #f, the value of the if expression is the value of Expression 2. Otherwise, the value of the if expression is the value of Expression 1. Evaluate (not "cookies")

23 January 2004CS 200 Spring Evaluate (not "cookies") Evaluation Rule 3. Application. a. Evaluate all the subexpressions “cookies” The quotes really matter here! Without them what would cookies evaluate to? b. Apply the value of the first subexpression to the values of all the other subexpressions (not v) evaluates to #t if v is #f, otherwise it evaluates to #f.(SICP, p. 19) So, (not “cookies”) evaluates to #f

23 January 2004CS 200 Spring Defining not (not v) evaluates to #t if v is #f, otherwise it evaluates to #f.(SICP, p. 19) (define (not v) (if v #f #t)

23 January 2004CS 200 Spring h (if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression 0. If it evaluates to #f, the value of the if expression is the value of Expression 1. Otherwise, the value of the if expression is the value of Expression 2. Evaluate (not "cookies") => #f So, value of if is value of Expression 2 => “starve”

23 January 2004CS 200 Spring DrScheme Languages If you didn’t set the language correctly in DrScheme, you got different answers! The “Beginning Student” has different evaluation rules –The rules are more complex –But, they gave more people what they expected

23 January 2004CS 200 Spring Comparing Languages Welcome to DrScheme, version 205. Language: Pretty Big (includes MrEd and Advanced). > + # Welcome to DrScheme, version 205. Language: Beginning Student. > + +: this primitive operator must be applied to arguments; expected an open parenthesis before the primitive operator name > ((lambda (x) x) 200) function call: expected a defined name or a primitive operation name after an open parenthesis, but found something else

23 January 2004CS 200 Spring (+ (abs (- (get-red color1) (get-red sample))) (abs (- (get-blue color1) (get-blue sample))) (abs (- (get-green color1) (get-green sample)))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) closer-color?

23 January 2004CS 200 Spring (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) (+ (abs (- (get-red color1) (get-red sample))) (abs (- (get-blue color1) (get-blue sample))) (abs (- (get-green color1) (get-green sample))))

23 January 2004CS 200 Spring (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) color1 sample color1 sample (lambda ( )

23 January 2004CS 200 Spring (+ (abs (- (get-red ) (get-red ))) (abs (- (get-blue ) (get-blue ))) (abs (- (get-green ) (get-green )))) (+ (abs (- (get-red color2) (get-red sample))) (abs (- (get-blue color2) (get-blue sample))) (abs (- (get-green color2) (get-green sample)))) (define (closer-color? sample color1 color2) (< )) (color-difference color1 sample) colora colorb colora colorb (lambda (colora colorb) (define color-difference )) (color-difference color2 sample)

23 January 2004CS 200 Spring (define color-difference (lambda (colora colorb) (+(abs (- (get-red colora) (get-red colorb))) (abs (- (get-green colora) (get-green colorb))) (abs (- (get-blue colora) (get-blue colorb)))))) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (color-difference color2 sample))) What if you want to use square instead of abs?

23 January 2004CS 200 Spring (define color-difference (lambda (cf) (lambda (colora colorb) (+ (cf (- (get-red colora) (get-red colorb))) (cf (- (get-green colora) (get-green colorb))) (cf (- (get-blue colora) (get-blue colorb))))))) (define (closer-color? sample color1 color2) (< (color-difference color1 sample) (color-difference color2 sample)))

23 January 2004CS 200 Spring (define color-difference (lambda (cf) (lambda (colora colorb) (+ (cf (- (get-red colora) (get-red colorb)) (cf (- (get-green colora) (get-green colorb)) (cf (- (get-blue colora) (get-blue colorb)))))))) (define (closer-color? sample color1 color2) (< ((color-difference square) color1 sample) ((color-difference square) color2 sample)))

23 January 2004CS 200 Spring The Patented RGB RMS Method /* This is a variation of RGB RMS error. The final square-root has been eliminated to */ /* speed up the process. We can do this because we only care about relative error. */ /* HSV RMS error or other matching systems could be used here, as long as the goal of */ /* finding source images that are visually similar to the portion of the target image */ /* under consideration is met. */ for(i = 0; i > size; i++) { rt = (int) ((unsigned char)rmas[i] - (unsigned char)image->r[i]); gt = (int) ((unsigned char)gmas[i] - (unsigned char) image->g[i]; bt = (int) ((unsigned char)bmas[i] - (unsigned char)image->b[i]; result += (rt*rt+gt*gt+bt*bt); } Your code should never look like this! Use new lines and indenting to make it easy to understand the structure of your code! (Note: unless you are writing a patent. Then the goal is to make it as hard to understand as possible.)

23 January 2004CS 200 Spring The Patented RGB RMS Method rt = rmas[i] - image->r[i]; gt = gmas[i] - image->g[i]; bt = bmas[i] - image->b[i]; result += (rt*rt + gt*gt + bt*bt); Patent requirements: 1.new – must not be previously available (ancient Babylonians made mosaics) 2.useful 3.nonobvious about ¼ of the class came up with this method! (most of rest used abs instead, which works as well)

23 January 2004CS 200 Spring CS200 PS Grading Scale  Gold Star – Excellent Work. You got everything I wanted on this PS. (only 1 on PS1)  Blue Star – Better than good work (4 on PS1).  Green Star – Good Work. You got most things on this PS, but some answers could be better.  Silver Star – Some problems. Make sure you understand the solutions on today’s slides. PS1 Average: 

23 January 2004CS 200 Spring No upper limit  - Double Gold Star: exceptional work! Better than I expected anyone would do.  - Triple Gold Star: Better than I thought possible (moviemosaic for PS1)  - Quadruple Gold Star: You have broken important new ground in CS which should be published in a major journal!  - Quintuple Gold Star: You deserve to win a Turing Award! (a fast, general way to make the best non-repeating photomosaic on PS1, or a proof that it is impossible)

23 January 2004CS 200 Spring Problem Set 2 Main ideas: recursion, procedures –We have covered everything you need after today Lots more code for you to write As we progress, problem sets will expect you to write more code on your own PS8 is “Do something worthwhile using things you have learned from this class.” (But will be a little bit more specific about what is expected.)

23 January 2004CS 200 Spring Charge Many, many important problems can be solved by recursive definitions Read GEB Chapter 5 before Monday’s class: lots of great stuff in there!