;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname module-05) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;; (inrange? x) return #true if x is between 10 and 30, ;; otherwise #false. ;; inrange?: Num -> Bool (define (inrange? x) (and (>= x 10) (<= x 30))) ;; keep-inrange: (listof Num) -> (listof Num) (define (keep-inrange L) (filter inrange? L)) (check-expect (keep-inrange (list 2 3 5 7.6 10.1 12 30 32)) (list 10.1 12 30)) ;; pythag?: (list Num Num Num) -> Bool (define (pythag? x) (and (< (first x) (second x)) (< (second x) (third x)) (= (+ (sqr (first x)) (sqr (second x))) (sqr (third x))))) (check-expect (pythag? (list 1 2 3)) #false) (check-expect (pythag? (list 3 4 5)) #true) (define (pythagoreans L) (filter pythag? L)) (check-expect (pythagoreans (list (list 1 2 3) (list 3 4 5) (list 5 12 13) (list 4 5 6))) (list (list 3 4 5) (list 5 12 13))) (check-expect (perfect? 25) #true) (check-expect (perfect? 21) #false) (define (perfect? n) (integer? (sqrt n))) (define (times-square L) (foldr * 1 (filter perfect? L) ) ) (check-expect (times-square (list 1 4 25 1 44357 234)) 100) ;(cons 6 (cons 7 (cons 42 '()))) (define (remove-second L) (cons (first L) (rest (rest L)))) (remove-second (list 2 4 6 0 1)) (define (double-first a b) (cons (* 2 a) b)) (define (double-each L) (foldr double-first '() L)) (double-each (list 2 3 5)) (define (keep-even-item a b) (cond [(even? a) (cons a b)] [else b])) (define (keep-evens L) (foldr keep-even-item '() L)) (keep-evens (list 1 2 3 4 5 6 7)) (define (cube-each L) (map (lambda (x) (* x x x)) L)) (check-expect (cube-each (list 2 3 5)) (list 8 27 125)) (define (keep4 L) (filter (lambda (s) (= 4 (string-length s))) L)) (keep4 (list "hello" "how" "are" "you?" "1234")) (define (multiply-each L n) (map (lambda (x) (* x n)) L)) (multiply-each (list 2 3 5) 4) (define (add-total L) (map (lambda (a) (+ a (foldr + 0 L))) L)) (add-total (list 2 3 5 10 2)) ((lambda (x y) (+ x y y)) 2 5) (define (discard-bad L lo hi) (filter (lambda (x) (and (<= lo x) (<= x hi))) L)) (discard-bad (list 12 5 20 10 15 25) 10 20) (define (add-n-item a b n) (cons (+ a n) b)) (define (add-n-each n L) (foldr (lambda (a b) (add-n-item a b n)) '() L)) (add-n-each 7 (list 2 4 8)) (define (times-row mult len) (map (lambda (x) (* x mult)) (range 1 (+ len 1) 1))) (times-row 3 4) (define (times-table len) (map (lambda (x) (times-row x len)) (range 1 (+ 1 len) 1))) (times-table 5) (define (change-1st-b-if-even-a a b) (cond [(even? a) (cons (+ 1 (first b)) (rest b))] [else (cons a b)])) (define (add-after-even L) (foldr change-1st-b-if-even-a '() L)) (check-expect (add-after-even (list 2 3 12 7)) (list 4 8)) (define (ponder new-item answer) (cond [(equal? new-item "ADD") (cons (+ (first answer) (second answer)) (rest (rest answer)))] [(equal? new-item "SQUARE") (cons (sqr (first answer)) (rest answer))] [(string? new-item) (cons (* 2 (first answer)) (rest answer))] [else (cons new-item answer)])) (define (muck-after-str L) (foldr ponder '() L)) (muck-after-str (list "ADD" 2 7 "SQUARE" 2 5))