lecture9

# lecture9 - Click to edit Master subtitle style 9/24/09...

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 Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the 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: find-closest- number (define (find-closest-number goal p) (if (null? (cdr p)) (car p) (if (< (abs (- goal (car p))) (abs (- goal (find-closest-number goal (cdr p)) ))) (car p) (find-closest-number goal (cdr p)) ))) 33 9/24/09 Avoiding Duplicate Work (define (pick-closest goal a b) (if (< (abs (- goal a)) (abs (- goal b))) a b)) (define (find-closest-number goal p) (if (null? (cdr p)) (car p) (pick-closest goal (car p) (find-closest-number goal (cdr p)) ))) 44 9/24/09 Generalizing find-closest- number • How would we implement find-closest-number-without-going- over? • What about find-closest-word? • ... The “closeness” metric should be a procedure parameter 55 9/24/09 find-minimizer (define (pick-minimizer f a b) (if (< (f a) (f b)) a b)) (define (find-minimizer f p) (if (null? (cdr p)) (car p) (pick-minimizer f (car p) (find-minimizer f (cdr p))))) find-minimizer 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 > (find-minimizer (lambda (n) (abs (- 1120 n))) (list 1130 1125 1203 1108)) 1125 > (find-minimizer (lambda (n) (- n)) (list 1 2 4 8 16)) 16 > (find-minimizer (lambda (n) (if (<= n 1120) (- 1120 n) 99999)) (list 1130 1125 1203 1108)) 1108 77 9/24/09 find-closest-number (define (find-closest-number goal p) (find-minimizer (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.

### Page1 / 32

lecture9 - Click to edit Master subtitle style 9/24/09...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online