This preview shows pages 1–10. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Click to edit Master subtitle style 9/24/09 cs1120 Fall 2009 David Evans http://www.cs.virginia.edu/evans Lecture 9: Mostly Not About Matter, Music, and Mayhem Richard Feynman’s van Alan Alda playing Richard Feynman in QED 11 9/24/09 Menu • Finishing find closest • Mapping on Lists • GEB Chapter V Paper about figuring out what is in the w 22 9/24/09 Last class: findclosest number (define (findclosestnumber goal p) (if (null? (cdr p)) (car p) (if (< (abs ( goal (car p))) (abs ( goal (findclosestnumber goal (cdr p)) ))) (car p) (findclosestnumber goal (cdr p)) ))) 33 9/24/09 Avoiding Duplicate Work (define (pickclosest goal a b) (if (< (abs ( goal a)) (abs ( goal b))) a b)) (define (findclosestnumber goal p) (if (null? (cdr p)) (car p) (pickclosest goal (car p) (findclosestnumber goal (cdr p)) ))) 44 9/24/09 Generalizing findclosest number • How would we implement findclosestnumberwithoutgoing over? • What about findclosestword? • ... The “closeness” metric should be a procedure parameter 55 9/24/09 findminimizer (define (pickminimizer f a b) (if (< (f a) (f b)) a b)) (define (findminimizer f p) (if (null? (cdr p)) (car p) (pickminimizer f (car p) (findminimizer f (cdr p))))) findminimizer takes two inputs: a procedure and a list. Its output is the element of the input list for which applying the procedure produces the lowest value. 66 9/24/09 Examples > (findminimizer (lambda (n) (abs ( 1120 n))) (list 1130 1125 1203 1108)) 1125 > (findminimizer (lambda (n) ( n)) (list 1 2 4 8 16)) 16 > (findminimizer (lambda (n) (if (<= n 1120) ( 1120 n) 99999)) (list 1130 1125 1203 1108)) 1108 77 9/24/09 findclosestnumber (define (findclosestnumber goal p) (findminimizer (lambda (n) (abs ( goal n))) p)) 88 9/24/09 Seen Anything Like This?...
View
Full
Document
This note was uploaded on 02/18/2010 for the course CS 150 taught by Professor Humphreys,g during the Fall '08 term at UVA.
 Fall '08
 Humphreys,G

Click to edit the document details