;; Lecture: Wednesday, Feb 26th ;; [X Y -> Y] Y [Listof X] -> Y (define (my-foldr op base lox) (cond [(empty? lox) base] [(cons? lox) (op (first lox) (my-foldr op base (rest lox)))])) ;; my-map : [X -> Y] [Listof X] -> [Listof Y] (define (my-map op lox) (cond [(empty? lox) empty] [(cons? lox) (cons (op (first lox)) (my-map op (rest lox)))])) ;; nums-to-lons : [Listof Number] - > [Listof [Listof Number]] (define (nums-to-lons lon) (my-map list lon)) (check-expect (nums-to-lons '(1 3 4 9 10)) (list (list 1) (list 3) (list 4) (list 9) (list 10))) ;; y-of-posns : [Listof Posn] -> [Listof Number] ;; extract the y coords of the posns in given list (define (y-of-posns lop) (my-map posn-y lop)) (check-expect (y-of-posns '()) '()) (check-expect (y-of-posns (list (make-posn 1 2) (make-posn 10 20))) (list 2 20)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; greater-than-5s : [Listof Number] -> [Listof Boolean] (define (greater-than-5s lon) (local (;; gt5 : Number -> Boolean ;; Is n greater than 5? (define (gt5 n) (> n 5))) (my-map gt5 lon))) (check-expect (greater-than-5s '()) '()) (check-expect (greater-than-5s '(1 3 5 7 9)) (list false false false true true)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; double : Number -> Number ;; multiply n by 2 (define (double n) (* 2 n)) ;; [Listof [Number -> Z]] -> [Listof Z] ;; apply all ops in lox to the number 5, collect results in output list (define (apply-all-to-5s lox) (local (;; apply-to-5 : [Number -> Z] -> Z ;; apply op to 5 (define (apply-to-5 op) (op 5))) ;; calling my-map with X = [Number -> Z] and Y = Z (my-map apply-to-5 lox))) (check-expect (apply-all-to-5s '()) '()) (check-expect (apply-all-to-5s (list add1 sub1 double zero? number->string string? number?)) (list 6 4 10 false "5" false true))