DOC PREVIEW
UVA CS 150 - Lecture 5: Recursing on Lists

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1David Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 5: Recursingon Lists2CS150 Fall 2005: Lecture 5: Recursing on ListsMenu• Implementing cons, car, cdr• PS1• List Recap• List RecursionEveryone who submitted a registration survey should have received an email yesterday with your PS2 partner. If you didn’t come talk to me after class.3CS150 Fall 2005: Lecture 5: Recursing on ListsImplementing cons, car and cdr(define (cons a b) (lambda (w) (if (w) a b)))(define (car pair) (pair #t)(define (cdr pair) (pair #f)Scheme provides primitive implementations for cons, car, and cdr. But, we could define them ourselves.4CS150 Fall 2005: Lecture 5: Recursing on ListsCS150 PS Grading ScaleGold Star – Excellent Work. You got everything I wanted on this PS. (No Gold Stars on PS1)Green Star – Better than good work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: 5CS150 Fall 2005: Lecture 5: Recursing on ListsNo 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)6CS150 Fall 2005: Lecture 5: Recursing on ListsQuestion 2• Without Evaluation Rules, Question 2 was “guesswork”• Now you know the Evaluation Rules, you can answer Question 2 without any guessing!27CS150 Fall 2005: Lecture 5: Recursing on Lists2d(100 + 100)Evaluation Rule 3. Application.a. Evaluate all the subexpressions100 <primitive:+> 100b. Apply the value of the first subexpression to the values of all the other subexpressionsError: 100 is not a procedure, weonly have apply rules for procedures!8CS150 Fall 2005: Lecture 5: Recursing on Lists2h(if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression2. Otherwise, the value of the if expression is the value of Expression1. Evaluate (not "cookies")9CS150 Fall 2005: Lecture 5: Recursing on ListsEvaluate (not "cookies")Evaluation Rule 3. Application.a. Evaluate all the subexpressions<primitive:not> “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 vis #f, otherwise it evaluates to #f. (SICP, p. 19)So, (not “cookies”) evaluates to #f10CS150 Fall 2005: Lecture 5: Recursing on ListsDefining not(not v) evaluates to #t if vis #f, otherwise it evaluates to #f.(SICP, p. 19)(define (not v) (if v #f #t)11CS150 Fall 2005: Lecture 5: Recursing on Lists2h(if (not "cookies") "eat" "starve") Evaluation Rule 4-if. Evaluate Expression0. If it evaluates to #f, the value of the if expression is the value of Expression1. Otherwise, the value of the if expression is the value of Expression2. Evaluate (not "cookies") => #fSo, value of if is value of Expression2=> “starve”12CS150 Fall 2005: Lecture 5: Recursing on ListsDrScheme 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 expected313CS150 Fall 2005: Lecture 5: Recursing on ListsComparing LanguagesWelcome to DrScheme, version 205.Language: Pretty Big (includes MrEd and Advanced).> +#<primitive:+>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 else14CS150 Fall 2005: Lecture 5: Recursing on Lists(+ (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)15CS150 Fall 2005: Lecture 5: Recursing on Lists(+ (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))))16CS150 Fall 2005: Lecture 5: Recursing on Lists(+ (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) (<))color1samplecolor1color1samplesample(lambda ( )17CS150 Fall 2005: Lecture 5: Recursing on Lists(+ (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)coloracolorbcoloracoloracolorbcolorb(lambda (colora colorb)(define color-difference))(color-difference color2 sample)18CS150 Fall 2005: Lecture 5: Recursing on Lists(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?419CS150 Fall 2005: Lecture 5: Recursing on Lists(define color-difference(lambda


View Full Document

UVA CS 150 - Lecture 5: Recursing on Lists

Documents in this Course
Objects

Objects

6 pages

Load more
Download Lecture 5: Recursing on Lists
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 5: Recursing on Lists and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 5: Recursing on Lists 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?