(require 2htdp/image)
;;; [List-of Number] -> [List-of Number]
;;; Add 10 to every element of the list.
(define (add-10-to-all lon)
(local ((define (add-10 n) (+ n 10)))
(map add-10 lon)))
;;; [List-of Number] Number -> [List-of Number]
;;; Add the number to every element of the list.
(define (add-to-all x lon)
(local ((define (add-x n) (+ n x)))
(map add-x lon)))
(check-expect (add-to-all 5 (list 1 10 40)) (list 6 15 45))
;;; my-map : [ X -> Y ] [List-of X] -> [List-of Y]
;;; Apply f to every elt in the list and collect results into a list.
;;; (list x1 ... xn) -> (list (f x1) ... (f xn))
(define (my-map f xs)
(cond [(empty? xs) empty]
[else (cons (f (first xs))
(my-map f (rest xs)))]))
(define (on-screen? p)
(and (<= 0 (posn-x p) 100)
(<= 0 (posn-y p) 100)))
(define (remove-offscreen-missiles lop)
(filter on-screen? lop))
;;; [X -> Boolean] [List-of X] -> [List-of X]
;;; filter out all elements of XS that fail the test.
(define (my-filter test xs)
(cond [(empty? xs) empty]
[(test (first xs)) (cons (first xs)
(my-filter test (rest xs)))]
[else (my-filter test (rest xs))]))
(check-expect (my-filter even? (list 3 1 4 1 5 9 2 6))
(list 4 2 6))
;;; [X Y -> Y] Y [List-of X] -> Y
(define (my-foldr f base xs)
(cond [(empty? xs) base]
[else (f (first xs)
(my-foldr f base (rest xs)))]))
;;; (foldr f base (cons x1 (cons x2 ... (cons xn empty))))
;;; -> (f x1 (f x2 ... (f xn base)))
(define (posn+img p i)
(place-image (circle 5 "solid" "red")
(posn-x p) (posn-y p)
i))
(define (segments+img segs img)
(foldr posn+img img segs))
(segments+img (list (make-posn 10 15) (make-posn 20 30) (make-posn 75 80)) (empty-scene 100 100))