David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything.

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 3: Rules of Evaluation.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Six compound procedures and higher-order procedures.
Administrivia Project 3 Part A due 3/29 (Monday after SB) Part B due 4/5 (a week after)  Everyone with a partner that wants a partner?  Extra Office.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
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.
Class 5: Constructing Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
מבוא מורחב 1 Review: scheme language things that make up scheme programs: self-evaluating 23, "hello", #t names +, pi combinations (+ 2 3) (* pi 4) special.
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.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
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 CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 36: Modeling Computing.
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.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
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 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!
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
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
CS 326 Programming Languages, Concepts and Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Functional Programming
This Lecture Substitution model
Lecture 22: Gödel’s Theorem CS200: Computer Science
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Class 24: Computability Halting Problems Hockey Team Logo
Class 31: Universal Turing Machines CS200: Computer Science
Lecture 2 מבוא מורחב.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
Lecture 3: Rules of Evaluation CS200: Computer Science
Class 26: Modeling Computing CS150: Computer Science
Introduction to the Lab
This Lecture Substitution model
Lecture 2 מבוא מורחב.
Lecture 23: Computability CS200: Computer Science
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: The Value of Everything

2 Lecture 4: Value of Everything Menu Problem Set 1 Evaluation Rules Return Problem Set 1 at end of class today Everyone should have received about your PS2 partner

3 Lecture 4: Value of Everything Question 2 Without Evaluation Rules, Question 2 was “guesswork” Once you know the Evaluation Rules, you can answer Question 2 without any guessing!

4 Lecture 4: Value of Everything 2d ( ) 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!

5 Lecture 4: Value of Everything 2h (if (not "cookies") "eat" "starve") Evaluation Rule 5: If. To evaluate an if expression: (a) Evaluate Expression Predicate. (b) If it evaluates to #f, the value of the if expression is the value of Expression Alternate. Otherwise, the value of the if expression is the value of Expression Consequent.

6 Lecture 4: Value of Everything 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 Application Rule 1: To apply a primitive, just do it. How do we evaluate an application of a primitive if we don’t know its pre-defined meaning?

7 Lecture 4: Value of Everything Defining not library procedure: (not obj) not returns #t if obj is false, and returns #f otherwise. (define (not v) (if v #f #t)

8 Lecture 4: Value of Everything 2h (if (not "cookies") "eat" "starve") Evaluate (not "cookies") => #f So, value of if is value of Expression 2 => “starve”

9 Lecture 4: Value of Everything brighter? (define brighter? (lambda (color1 color2) (if (> (+ (get-red color1) (get-green color1) (get-blue color1) ) (+ (get-red color2) (get-green color2) (get-blue color2)) #t #f))) Is this correct? Maybe...but very hard to tell. Your code should appear in a way that reveals its structure

10 Lecture 4: Value of Everything (define brighter? (lambda (color1 color2) (if (> (+ (get-red color1) (get-green color1) (get-blue color1)) (+ (get-red color2) (get-green color2) (get-blue color2)) #t #f))) Use [Tab] in DrScheme to line up your code structurally!

11 Lecture 4: Value of Everything Iffy Proposition (if Expression #t #f) == Expression Is this always true? (if “cookies” #t #f)

12 Lecture 4: Value of Everything Brighter brighter? ? (define brighter? (lambda (color1 color2) (> (+ (get-red color1) (get-green color1) (get-blue color1)) (+ (get-red color2) (get-green color2) (get-blue color2)))))

13 Lecture 4: Value of Everything Brighter brighter? ? (define brightness (lambda (color) (+ (get-red color) (get-green color) (get-blue color)))) (define brighter? (lambda (color1 color2) (> (brightness color1) (brightness color2)))

14 Lecture 4: Value of Everything Believable brighter? ? (make-color ) (make-color )

15 Lecture 4: Value of Everything Color Absorbed

16 Lecture 4: Value of Everything Cognitive Scientist’s Answer (define brightness (lambda (color) (+ (* (get-red color)) (* (get-green color)) (* (get-blue color))))) (define brighter? (lambda (color1 color2) (> (brightness color1) (brightness color2)))

17 Lecture 4: Value of Everything (+ (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? (Green Star version)

18 Lecture 4: Value of Everything (+ (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))))

19 Lecture 4: Value of Everything (+ (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 ( )

20 Lecture 4: Value of Everything (+ (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)

21 Lecture 4: Value of Everything (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?

22 Lecture 4: Value of Everything (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 Lecture 4: Value of Everything (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)))

24 Lecture 4: Value of Everything 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.)

25 Lecture 4: Value of Everything 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 ~1/4 of you came up with this method! (most of rest used abs instead, which works as well)

26 Lecture 4: Value of Everything CS150 PS Grading Scale  Gold Star – Excellent Work. (No Gold Stars on PS1)  Green Star – You got everything I wanted.  Blue 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: 

27 Lecture 4: Value of Everything 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)

28 Lecture 4: Value of Everything What should you do if you can’t get your code to work? Keep trying – think of alternate approaches Get help from the ACs and your classmates But, if its too late for that... –In your submission, explain what doesn’t work and as much as you can what you think is right and wrong

Evaluation Rules

30 Lecture 4: Value of Everything Primitive Expressions Expression ::= PrimitiveExpression PrimitiveExpression ::= Number PrimitiveExpression ::= #t | #f PrimitiveExpression ::= Primitive Procedure Evaluation Rule 1: Primitive. If the expression is a primitive, it evaluates to its pre-defined value. > + #

31 Lecture 4: Value of Everything Name Expressions Expression ::= NameExpression NameExpression ::= Name Evaluation Rule 2: Name. If the expression is a name, it evaluates to the value associated with that name. > (define two 2) > two 2

32 Lecture 4: Value of Everything Definitions Definition ::= (define Name Expression) Definition Rule. A definition evaluates the Expression, and associates the value of Expression with Name. > (define dumb (+ + +)) +: expects type as 1st argument, given: # ; other arguments were: # > dumb reference to undefined identifier: dumb

33 Lecture 4: Value of Everything Evaluation Rule 5: If Expression ::= (if Expression Predicate Expression Consequent Expression Alternate ) Evaluation Rule 5: If. To evaluate an if expression: a. Evaluate the predicate expressions. b. If it evaluates to #f, the value of the if expression is the value of alternate expression. Otherwise, the value of the if expression is the value of consequent expression.

34 Lecture 4: Value of Everything Application Expressions Expression ::= ApplicationExpression ApplicationExpression ::= (Expression MoreExpressions) MoreExpressions ::= ε | Expression MoreExpressions Evaluation Rule 3: Application. To evaluate an application expression: a. Evaluate all the subexpressions; b. Then, apply the value of the first subexpression to the values of the remaining subexpressions.

35 Lecture 4: Value of Everything Rules for Application 1.Primitive. If the procedure to apply is a primitive, just do it. 2.Constructed Procedure. If the procedure is a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

36 Lecture 4: Value of Everything Constructing Procedures: Lambda Expression ::= ProcedureExpression ProcedureExpression ::= (lambda (Parameters) Expression) Parameters ::= ε | Name Parameters Evaluation Rule 4: Lambda. Lambda expressions evaluate to a procedure that takes the given Parameters as inputs and has the Expression as its body.

37 Lecture 4: Value of Everything Applying Constructed Procedures Application Rule 2: Constructed Procedure. If the procedure is a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value.

38 Lecture 4: Value of Everything Applying Constructed Procedures Application Rule 2: Constructed Procedure. If the procedure is a constructed procedure, evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument expression value. > ((lambda (n) (+ n 1)) 2) ((lambda (n) (+ n 1)) 2) Evaluation Rule 3a: evaluate the subexpressions Evaluation Rule 3b, Application Rule 2 (+ 2 1) Evaluation Rule 3a, 3b, Application Rule 1 3

39 Lecture 4: Value of Everything Lambda Example: Tautology Function (lambda () #t) > ((lambda () #t) 150) # : expects no arguments, given 1: 150 > ((lambda () #t)) #t > ((lambda (x) x) 150) 150 make a procedure with no parameters with body #t

40 Lecture 4: Value of Everything Eval Apply Eval and Apply are defined in terms of each other. Without Eval, there would be no Apply, Without Apply there would be no Eval!

41 Lecture 4: Value of Everything Now You Know All of Scheme! Once you understand Eval and Apply, you can understand all Scheme programs! Except: –There are many primitives, need to know their predefined meaning –There are a few more special forms (like if) –We have not define the evaluation rules precisely enough to unambiguously understand all programs (e.g., what does “value associated with a name” mean?)

42 Lecture 4: Value of Everything Charge (In theory) You now know everything you need for PS2, PS3 and PS4 Friday: Programming with Data Next week - lots of examples of: –Programming with procedures, data –Recursive definitions But, if you understand the Scheme evaluation rules, you know it all already!