;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-beginner-reader.ss" "lang")((modname comp211-lab01) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; Develop a program count-positive which takes a list of numbers and returns a count of ; how many are positive, i.e., greater than zero. ; Data Definition ; A list-of-numbers is either: ; - empty, or ; - (cons number list-of-numbers) ; Examples ; empty ; (cons 42 empty) ; (cons -42 empty) ; (cons 0 empty) ; (cons 23 (cons 42 empty)) ; (cons -17 (cons 23 (cons 42 empty))) ; (cons -17 (cons 23 (cons 42 (cons -13.37 empty)))) ; (cons 0 (cons -17 (cons 23 (cons 42 (cons -13.37 empty))))) ; Template #| (define (f ... lon ...) (cond [(empty? lon) ...] [(cons? lon) ... (first lon) ... (f ... (rest lon) ...)])) |# ; Contract: count-positive: list-of-numbers -> natural ; Purpose: Takes a list of numbers and returns a count of how many are ; positive, i.e., greater than zero. ; Examples: ; (count-positive (cons 0 (cons -17 (cons 23 (cons 42 (cons -13.37 empty)))))) -> 2 (define (count-positive lon) (cond [(empty? lon) 0] [(cons? lon) (cond [(> (first lon) 0) (+ 1 (count-positive (rest lon)))] [else (count-positive (rest lon))])])) "count-positive tests" (check-expect (count-positive empty) 0) (check-expect (count-positive (cons 42 empty)) 1) (check-expect (count-positive (cons -42 empty)) 0) (check-expect (count-positive (cons 0 empty)) 0) (check-expect (count-positive (cons 23 (cons 42 empty))) 2) (check-expect (count-positive (cons -17 (cons 23 (cons 42 empty)))) 2) (check-expect (count-positive (cons -17 (cons 23 (cons 42 (cons -13.37 empty))))) 2) (check-expect (count-positive (cons 0 (cons -17 (cons 23 (cons 42 (cons -13.37 empty)))))) 2) ; Develop a program map-add which takes a number and a list of numbers and returns a ; list of numbers, where the resulting list is constructed by adding the given number to ; each element in the list. ; map-add: number lon -> lon ; Purpose: Takes a number and a list of numbers and returns a ; list of numbers, where the resulting list is constructed by adding the given number to ; each element in the list. ; Examples: ; (map-add 3 (cons 1 (cons 4 (cons 8 empty)))) -> (cons 4 (cons 7 (cons 11 empty))) (define (map-add number lon) (cond [(empty? lon) empty] [(cons? lon) (cons (+ number (first lon)) (map-add number (rest lon)))])) "map-add tests" (check-expect (map-add 5 empty) empty) (check-expect (map-add 3 (cons 42 empty)) (cons 45 empty)) (check-expect (map-add 17 (cons 23 (cons 42 empty))) (cons 40 (cons 59 empty))) (check-expect (map-add 10 (cons -17 (cons 23 (cons 42 empty)))) (cons -7 (cons 33 (cons 52 empty)))) (check-expect (map-add -1.5 (cons -17 (cons 23 (cons 42 (cons -1337 empty))))) (cons -18.5 (cons 21.5 (cons 40.5 (cons -1338.5 empty))))) ; Develop a program filter-positive which takes a list of numbers and returns a list of ; all those numbers which are positive. ; (cons 42 empty) ; (cons -42 empty) ; (cons 0 empty) ; (cons 23 (cons 42 empty)) ; (cons -17 (cons 23 (cons 42 empty))) ; (cons -17 (cons 23 (cons 42 (cons -13.37 empty)))) ; (cons 0 (cons -17 (cons 23 (cons 42 (cons -13.37 empty))))) ; filter-positive: lon -> lon ; Purpose: Takes a list of numbers and returns a list of ; all those numbers which are positive. ; Examples: ; (filter-positive (cons 42 empty)) -> (cons 42 empty) ; (filter-positive (cons -17 (cons 23 (cons 0 (cons 42 (cons -13.37 empty))))) -> ; (cons 23 (cons 42 empty)) (define (filter-positive lon) (cond [(empty? lon) empty] [(cons? lon) (cond [(> (first lon) 0) (cons (first lon) (filter-positive (rest lon)))] [else (filter-positive (rest lon))])])) "filter-positive tests" (check-expect (filter-positive empty) empty) (check-expect (filter-positive (cons 42 empty)) (cons 42 empty)) (check-expect (filter-positive (cons -17 (cons 23 (cons 42 empty)))) (cons 23 (cons 42 empty))) (check-expect (filter-positive (cons -17 (cons -23 (cons -42 empty)))) empty) (check-expect (filter-positive (cons -17 (cons 0 (cons 42 empty)))) (cons 42 empty)) ; Write the function positive-quadrant?, which takes in a single posn ; and returns true only when its components are both positive. ; positive-quadrant?: posn -> boolean ; Purpose: Takes in a single posn ; and returns true only when its components are both positive. ; Examples: ; (positive-quadrant? (make-posn 2 3)) -> true (define (positive-quadrant? posn) (and (> (posn-x posn) 0) (> (posn-y posn) 0))) "positive-quadrant?" (check-expect (positive-quadrant? (make-posn 3 4)) true) (check-expect (positive-quadrant? (make-posn -3 4)) false) (check-expect (positive-quadrant? (make-posn -3 -4)) false) ; Develop count-positive-quadrant, which takes a list of posns and returns a count of ; those in the upper-right quadrant. ; count-positive-quadrant: lop -> natural ; Purpose: Takes a list of posns and returns a count of ; those in the upper-right quadrant. ; Examples: ; (count-positive-quadrant (cons (make-posn 3 4) empty)) -> 1 (define (count-positive-quadrant lop) (cond [(empty? lop) 0] [(cons? lop) (cond [(positive-quadrant? (first lop)) (+ 1 (count-positive-quadrant (rest lop)))] [else (count-positive-quadrant (rest lop))])])) "count-positive-quadrant tests" (check-expect (count-positive-quadrant empty) 0) (check-expect (count-positive-quadrant (cons (make-posn 3 4) empty)) 1) (check-expect (count-positive-quadrant (cons (make-posn -3 4) empty)) 0) (check-expect (count-positive-quadrant (cons (make-posn -3 4) (cons (make-posn 3 4) empty))) 1) (check-expect (count-positive-quadrant (cons (make-posn -3 4) (cons (make-posn 3 4) (cons (make-posn 13 37) empty)))) 2)