12 POINTS |
Problem 4. Your company keeps track of customers with a customer list. For each customer, your company records the customer name (a string) and the customer's current outstanding balance (a number).
It is your task to develop a program that identifies customers with high outstanding balances. Specifically, your program consumes a list of customers and a threshold amount th. It produces a list of customers whose outstanding balance is at least th.
Solution:
(define-struct customer (name balance)) ;; Customer = (make-customer String Number) ;; [PT 2, one for struct definition and one for dd] ;; A LOC is one of: ;; -- empty ;; -- (cons Customer LOC) ;; [PT 1, for data def of list-of-customers] ;; toomuch : LOC Number -> LOC [PT 1] ;; extract those customer records ;; whose balance exceeds th [PT 1] (define (toomuch th aloc) (cond ;; [PT 2, for cond lines] [(empty? aloc) empty] ;; [PT 1, for empty] [else (cond ;; [PT 3, two for two cases, one for correctness] [(>= (customer-balance (first aloc)) th) (cons (first aloc) (toomuch th (rest aloc)))] [else (toomuch th (rest aloc))])])) ;; Examples/tests: [PT 1, for examples/tests] (equal? (toomuch 15 (cons (make-customer "A" 10) (cons (make-customer "B" 20) (cons (make-customer "C" 15) empty)))) (cons (make-customer "B" 20) (cons (make-customer "C" 15) empty)))