(define title "Using Abstractions, Part II") (define description "lambda the one-liner") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM ;; paint : [Listof Posn] -> Image ;; paint red spots at the given posn-s on 100 x 100 canvas ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (paint l) (local ((define (bob p i) (place-image dot (posn-x p) (posn-y p) i)) (define dot (circle 3 'solid 'red))) (foldr bob (empty-scene 100 100) l))) (equal? (paint (list (make-posn 20 20))) (place-image (circle 3 'solid 'red) 20 20 (empty-scene 100 100))) ;; foldr : [X Y -> Y] Y [Listof X] -> Y ;; what are X and Y? X = Posn, Y = Image ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM (define WIDTH 100) (define HEIGHT 100) ;; remove-bad-balls : [Listof Posn] -> [Listof Posn] ;; remove the Posns that are outside of [0,WIDTH] x [0,HEIGHT] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (remove-offscreen-balls lop) (local ((define (good? p) (and (<= 0 (posn-x p) WIDTH) (<= 0 (posn-y p) HEIGHT)))) (filter good? lop))) (equal? (remove-offscreen-balls (list (make-posn (/ WIDTH 2) (/ HEIGHT 2)) (make-posn (+ WIDTH 2) HEIGHT))) (list (make-posn (/ WIDTH 2) (/ HEIGHT 2)))) ;; filter [X -> Boolean] [Listof X] -> [Listof X] ;; X = Posn ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; QUIZ ;; widths : [Listof Image] -> [Listof Number] ;; determines the width of each image on the list ;; HINT: image-width : Image -> Number ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM ;; intersect : [Listof X] [Listof X] -> [Listof X] ;; which elements are in both lists? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (intersect.v1 s t) (cond [(empty? s) empty] [else (local ((define fst (first s)) (define rst (intersect (rest s) t))) (if (member fst t) (cons fst rst) rst))])) (define (intersect.v2 s t) (local ((define (alice fst rst) (if (member fst t) (cons fst rst) rst))) (foldr alice empty s))) (define (intersect.v3 s t) (local [(define (george x) (member x s))] (filter george t))) (equal? (intersect '(a b c) '(c b)) '(b c)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM ---- do this on the COMPUTER! ---- ;; union : [Listof X] [Listof X] -> [Listof X] ;; which elements are in either list, but not shared (define (union.v1 s t) (cond [(empty? s) t] [else (local ((define fst (first s)) (define rst (union (rest s) t))) (if (member fst t) rst (cons fst rst)))])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; rewrite with a loop (define (union s t) (local ((define (carl f r) (if (member f t) r (cons f r)))) (foldr carl t s))) (equal? (union '(a b c) '(d a e f)) '(b c d a e f)) (equal? (union '(a b c) '(d a a f)) '(b c d a a f)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM: ;; Sexp is one of: ;; -- Symbol ;; -- [Listof Sexp] ;; flatten : Sexp -> [Listof Symbol] ;; extract all leafs in order (define (flatten t) (cond [(symbol? t) (list t)] [else (apply append (map flatten t))])) (equal? (flatten '(((a) b) c d ((e)))) '(a b c d e))