Class 5: Constructing Procedures David Evans cs1120 Fall 2009.

Slides:



Advertisements
Similar presentations
This is a speed quiz. You will have just 10 seconds to get the right answer. Dogs Dad will appear when you have 3 seconds left Have you got what it takes???
Advertisements

Class 3: Rules of Evaluation David Evans cs1120 Fall 2009.
Class 6: Programming with Data David Evans cs1120 Fall 2009.
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
Logic & program control part 3: Compound selection structures.
Intro to CIT 594
Hard problems November Administrivia Ps 8 –Handed out Tuesday (12/4) –Due Tuesday (12/11) or Tuesday (12/18) your choice Ps 9 –Handed out Thursday.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
Broken Chain Gary Soto.
Six compound procedures and higher-order procedures.
Intro to CIT 594
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
Thirteen conditional expressions: letting programs make “decisions”
Today’s quiz on 8.2 B Graphing Worksheet 2 will be given at the end of class. You will have 12 minutes to complete this quiz, which will consist of one.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Java: Chapter 1 Computer Systems Computer Programming II.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Introducing Recursive Definitions.
Class 37: Uncomputability David Evans University of Virginia cs1120 Halting Problems Hockey Team.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 2: Orders of Growth
Prof. Matthew Hertz WTC 207D /
1 Project Information and Acceptance Testing Integrating Your Code Final Code Submission Acceptance Testing Other Advice and Reminders.
Best (Online) Practices for Teaching MTH Dr. Laurie Burton Mathematics Department TOTOM—September 11, 2015.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 4: Recursive Definitions.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Daily Announcements CS 202, Spring 2007 Aaron Bloomfield.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
22/11/ Selection If selection construct.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
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.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
WELCOME 2 ELA Warm-Up: 1.Come in and find your seat (name is on it) 2.Write your full name at the top of your index card. 3.Sit in your seat and wait for.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
REMINDER: If you haven’t yet passed the Gateway Quiz, make sure you take it this week! (You can find more practice quizzes online in the Gateway Info menu.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 5: Recursion Beware the Lizards!
CNG 140 C Programming (Lecture set 3)
Class 8: Procedure Practice
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 14: Intractable Problems CS150: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Class 24: Computability Halting Problems Hockey Team Logo
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 23: Computability CS200: Computer Science
Presentation transcript:

Class 5: Constructing Procedures David Evans cs1120 Fall 2009

Menu Problem Set 1 – Evaluation – Constructing Procedures – Returning Problem Sets Procedure Return Problem Set 1 at end of class today 2

From Question 1/2: (if (not "cookies") "eat" "starve") Evaluation Rule 5: If. To evaluate an if expression: (a)Evaluate the predicate expression (b)If it evaluates to false, the value of the if expression is the value of the alternate expression. Otherwise, the value of the if expression is the value of consequent expression. This means every non-false value is considered “true”. 3

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? 4

5

Defining not not: Value  Boolean not is a built-in procedure that takes one input value and outputs a Boolean. not returns #t if obj is false, and #f otherwise. (define (not v) (if v #f #t)) 6

(if (not "cookies") "eat" "starve") (not "cookies") => #f So, value of the if expression is value of Expression 2 => “starve” 7

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 8

(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! 9

Iffy Proposition (if Expression #t #f) == Expression Is this always true? (if “cookies” #t #f) 10

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))))) 11

Brighter brighter? ? (define brightness (lambda (color) (+ (get-red color) (get-green color) (get-blue color)))) (define brighter? (lambda (color1 color2) (> (brightness color1) (brightness color2))) 12

Believable brighter? ? (make-color ) (make-color ) 13

Color Absorbed 14

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))) 15

(+ (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? 16

(+ (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)))) 17

(+ (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 ( ) 18

(+ (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) 19

(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? 20

(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))) 21

(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))) 22

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

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/3 of you came up with this method! (most of rest used abs instead, which works as well) 24

cs1120 PS Grading Scale  Gold Star – Excellent Work. You got everything we wanted out of this assignment.  Green Star – You got most things on this PS, but some answers could be better.  Silver Star – Some serious problems.  Red Star – Didn’t get much of what you should have out of this problem set. PS1 Average:  25 What you turn in is much more important than passing the automatic tests.

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)moviemosaic  - Quadruple Gold Star: You have broken important new ground in CS which should be published in a major conference!  - 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) 26

Pledges on PS1 About ¼ of the class wrote: “On my honor as a student, I have neither given nor received aid on this assignment.” 27 I will do what I can to help my fellow classmates learn. Except when specifically instructed not to, this means when other students ask me for help, I will attempt to provide it. I will look at their answers and discuss what I think is good or bad about their answers. I will help others improve their work, but will not give them my answers directly. I will try to teach them what they need to know to discover solutions themselves. From the Course Pledge you signed:

Meta-Lessons from PS1 Start early Take advantage of Help Hours and Office Hours – Sunday 6-8:30 (Olsson 001) – Monday 12-1:30 (Small), 1:30-2:30 (236A), 4-6:30 (Small) – Tuesdays 10:30-11:30 (Wilsdorf Café); 2-3, 3:30-5, 7-8:30 (Small) – Wednesdays 6-8:30 (Olsson 001) Read the PS1 comments 28

Pre-Charge Problem Set 2: even more “fun” than PS1! – Partner assignments on web Before Monday: read through Section 5.4 Monday: – Maybe a quiz to check you read the PS1 comments and reading assignments – Programming with Data: Pairs and Lists All next week: Recursive Procedures 29

Returning Problem Sets Problem Input: unordered set of cs1120 students Output: cs1120 students in lexicographic order by UVa ID 30 What is a good algorithm for getting all of you in order by UVa ID?

31