;; 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 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/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) ; Template Instantiation #| (define (count-positive lon) (cond [(empty? lon) ...] [(cons? lon) ... (first lon) ... (count-positive (rest lon)) ...])) |# ; Code (define (count-positive lon) (cond [(empty? lon) 0] [(cons? lon) (if (positive? (first lon)) (add1 (count-positive (rest lon))) (count-positive (rest lon)))])) ; 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: (map-add n lon) returns a list constructed by adding n to each element in lon. ; Examples/Tests: (check-expect (map-add 5 empty) empty) (check-expect (map-add 3 (list 42)) (list 45)) (check-expect (map-add 17 (list 23 42)) (list 40 59)) (check-expect (map-add 10 (list -17 23 42)) (list -7 33 52)) (check-expect (map-add -1.5 (list -17 23 42 -1337)) (list -18.5 21.5 40.5 -1338.5)) (define (map-add number lon) (cond [(empty? lon) empty] [(cons? lon) (cons (+ number (first lon)) (map-add number (rest lon)))])) ; Develop a program filter-positive which takes a list of numbers and returns a list of ; all those numbers which are positive. ; filter-positive: lon -> lon ; Purpose: (filter-positive lon) returns a list of all the numbers in lon that are positive. ; Examples/Tests: (check-expect (filter-positive empty) empty) (check-expect (filter-positive (list 42)) (list 42)) (check-expect (filter-positive (list -17 23 42)) (list 23 42)) (check-expect (filter-positive (list -17 -23 -42)) empty) (check-expect (filter-positive (list -17 0 42)) (list 42)) ; Template Instantiation #| (define (filter-positive lon) (cond [(empty? lon) ...] [(cons? lon) ... (first lon) ... (filter-positive (rest lon)) ...])) |# ; Code (define (filter-positive lon) (cond [(empty? lon) empty] [(cons? lon) (if (positive? (first lon)) (cons (first lon) (filter-positive (rest lon))) (filter-positive (rest lon)))])) ; 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/Tests: (check-expect (positive-quadrant? (make-posn 3 4)) true) (check-expect (positive-quadrant? (make-posn 0 0)) false) (check-expect (positive-quadrant? (make-posn 0 1)) false) (check-expect (positive-quadrant? (make-posn 1 0)) false) (check-expect (positive-quadrant? (make-posn -3 4)) false) (check-expect (positive-quadrant? (make-posn -3 -4)) false) (check-expect (positive-quadrant? (make-posn 3 -4)) false) ; Template Instantiation #| (define (positive-quadrant? posn) ... (posn-x posn) 0) ... (posn-y posn) ... ) |# ; Code (define (positive-quadrant? posn) (and (positive? (posn-x posn)) (positive? (posn-y posn)))) ; 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: (count-positive-quadrant lop) returns a count of the posns in lop in the upper-right quadrant. ; Examples/Tests (check-expect (count-positive-quadrant empty) 0) (check-expect (count-positive-quadrant (list (make-posn 3 4))) 1) (check-expect (count-positive-quadrant (list (make-posn -3 4))) 0) (check-expect (count-positive-quadrant (list (make-posn -3 4) (make-posn 3 4))) 1) (check-expect (count-positive-quadrant (list (make-posn -3 4) (make-posn 3 4) (make-posn 13 37))) 2) ; Template Instantiation #| (define (count-positive-quadrant lop) (cond [(empty? lop) ...] [(cons? lop) ... (first lop)) ... (count-positive-quadrant (rest lop)) ... ])) |# ; Code (define (count-positive-quadrant lop) (cond [(empty? lop) 0] [(cons? lop) (if (positive-quadrant? (first lop)) (add1 (count-positive-quadrant (rest lop))) (count-positive-quadrant (rest lop)))]))