;1. ;Write down a data definition for lists of numbers, ;and examples of lists of numbers ; list of numbers is ; - empty or ; - (cons f,r) ; where f is a number ; where r is a list of numbers ;Examples (cons 3 empty) empty (cons 6 (cons 3 (cons 8 empty))) ;Template #| (define (aFunc lon) (cond [(empty? lon) ....] [(cons? lon) .... (first lon)....(aFunc (rest lon))] )) |# ;(This template could instead occur at the start of the next problem, ;but it should occur (exactly) once.) ;Now, develop the following programs that consume a list of real numbers: ; 1a. ;; sum: list of numbers -> number ;; returns the sum of the numbers in the list (define (sum lon) (cond [(empty? lon) 0] [(cons? lon) (+ (first lon) (sum (rest lon)))] )) ; Test cases 'testing-sum (= (sum empty) 0) (= (sum (cons 4 (cons 6 (cons 3 empty)))) 13) (= (sum (cons 2 empty)) 2) (= (sum (cons -2 (cons -6 empty))) -8) ; 1b. ;;any-negative?: list of numbers -> boolean ;; Are there any negative numbers in the list? ;; number in the list (define (any-negative? lon) (cond [(empty? lon) false] [(cons? lon) (or (negative? (first lon)) (any-negative? (rest lon)))])) ;; negative?: number --> boolean ;; Returns true if the number is negative. (define (negative? n) (< n 0)) ;test cases 'testing-any-negative? (boolean=? (any-negative? empty) false) (boolean=? (any-negative? (cons 0 empty)) false) (boolean=? (any-negative? (cons -3 empty)) true) (boolean=? (any-negative? (cons 7 (cons 0 empty))) false) (boolean=? (any-negative? (cons 2 (cons -19 (cons 7 (cons 0 empty))))) true) ;1c. ;;all-nonnegative?: list of numbers -> boolean ;; Are all the numbers in nums non-negative? ;; Note that this is true exactly if there are *not* any-negative. ;; (define (all-nonnegative? nums) (not (any-negative? nums))) ; Test cases 'testing-all-nonnegative? (boolean=? (all-nonnegative? empty) true) (boolean=? (all-nonnegative? (cons 0 empty)) true) (boolean=? (all-nonnegative? (cons -3 empty)) false) (boolean=? (all-nonnegative? (cons -7 (cons -10 empty))) false) (boolean=? (all-nonnegative? (cons 7 (cons 0 empty))) true) (boolean=? (all-nonnegative? (cons 2 (cons -19 (cons 7 (cons 0 empty))))) false) ;1d. ;; count-multiples: list of numbers, number -> number ;; how many numbers in the list are multiples of a-num (define (count-multiples a-lon a-num) (cond [(empty? a-lon) 0] [(cons? a-lon) (cond [(= (modulo (first a-lon) a-num) 0) (+ 1 (count-multiples (rest a-lon) a-num))] [else (count-multiples (rest a-lon) a-num)])])) ;Test cases 'testing-count-multiples (= (count-multiples empty 100) 0) (= (count-multiples (cons 4 empty) 2) 1) (= (count-multiples (cons 3 (cons 9 (cons 4 empty))) 3) 2) (= (count-multiples (cons 3 (cons 9 (cons 4 (cons 8 empty)))) 5) 0) (= (count-multiples (cons 3 (cons 9 (cons 4 (cons 8 empty)))) 1) 4) ; 2. ;Once a year, the ;local telephone company publishes a directory. For our purposes, the ;directory is a list of entries. An entry consists of a symbol, called ;the key, and a phone number, represented as a number. ;Note that SWBell probably doesn't use a simple number -- ;they think of a number as an area code, an exchange, ;and an extension (each of which are numbers). ;Thus they probably have a structure with three fields. ;Note that this makes it easier to write, say, ;change-area-code, which changes all 281 numbers to 713 numbers. ;Note: when talking about types of data, ;this class is precise. ;Thus be aware that, for this problem, directory ;and entry are technical terms, ;used exactly where intended ; 2a. ;Write out the ;data definitions, examples, and template ;pertaining to this simple online phone directory. You should ;have one data definition for entries, and a second data definition for ;directories. ;; An entry is: ;; (make-entry symbol val) ;; An entry is structure consisting of ;; - key which identifies the entry, and ;; - val which is the interesting part -- the datum ;; associated with the key. (define-struct entry (key val)) ;Examples: (make-entry 'malcolm 4041) (make-entry 'operator 0000) (make-entry 'campos 6000) #| Template for an entry: ;; .. : entry --> .. ;; ;; (define (handle-entry an-entry) ..(entry-key an-entry)..(entry-val an-entry)..) |# ; A telephone directory is a list of entries which is either ; - empty or ; - cons(f,r) ; where f is an entry ; where r is a list of entries ;Examples (define malc (make-entry 'malcolm 4041)) (define police (make-entry 'campos 6000)) (define help (make-entry 'operator 0000)) (define dir0 empty) (define dir1 (cons (make-entry 'campos 6000) empty)) (define dir2 (cons (make-entry 'operator 0000) dir1)) (define dir3 (cons (make-entry 'malcolm 4041) dir2)) ;; Template #| (define (aFunc a-dir).... (cond [(empty? a-dir) ....] [(cons? a-dir) ... (first a-dir)... (aFunc (rest a-dir))])) |# ;2b. ;; lookup: symbol, list of entries -> boolean or entry ;; returns the matching entry of the key if there is a match; otherwise ;; it just returns false (define (lookup a-dir key) (cond [(empty? a-dir) false] [(cons? a-dir) (cond [(match-key? key (first a-dir)) (first a-dir)] [else (lookup (rest a-dir) key)])])) ;; match-key?: symbol, entry --> boolean ;; (define (match-key? a-key an-entry) (symbol=? a-key (entry-key an-entry))) ;test cases 'testing-match (boolean=? (match-key? 'malcolm malc) true) (boolean=? (match-key? 'campos malc) false) ; test cases 'testing-lookup (boolean=? (lookup dir0 'campos) false) (equal? (lookup dir1 'campos) police) (boolean=? (lookup dir1 'malcolm) false) (equal? (lookup dir3 'malcolm) malc) ;2c. ;; An entry-or-false is ;; - false, or ;; - an entry. #| Template: ;; .. : entry-or-false --> .. (define (f eof) (cond [(boolean? eof) ..(process-entry eof)..] [else ..eof..])) |# ;You should have a template for entry-or-false, ;but if you don't we won't take off points ;(since the function you'll write below doesn't ;exactly take one in). ;; lookup-elvis: directory -> number or symbol ;; returns Elvis's number if he is listed in the directory ;; or 'not-found if he's not listed. (define (lookup-elvis a-dir) (cond [(boolean? (lookup a-dir 'elvis)) 'not-found] [else (entry-val (lookup a-dir 'elvis))])) ; test cases 'testing-lookup-elvis (symbol=? (lookup-elvis empty) 'not-found) (= (lookup-elvis (cons malc (cons (make-entry 'elvis 3843) dir2))) 3843)