Problem 4. Your small but fast-growing software consulting firm has been approached by Mr. S.A. Dam, whose small business sells oil and takes in bribes. His accounting system is extremely simple. He just keeps two lists: the amounts for oil coupons given away and the amounts for bribes taken in. To simplify the accounting, both lists are dollar amounts; it simplifies communication with a certain place in New Uorque.
Mr. K.O. Fee, your manager, has figured out that the best data representation of this accounting system is this:
(define-struct account (in out)) ;; An Account is a structure: ;; (make-account LOAS LOAS) ;; An LOAS is one of: ;; -- empty ;; -- (cons Number LOAS) ;; interpretation: each number represents a number of dollars
He asked you to design a program that computes the surplus of an account. That is, it consumes an account and produces the difference between the money taken in and the money that went out. The number is positive if Mr. Dam made a profit so far and negative if not.
Solution:
;; Grader: Matthias F. ;; two function definitions: 1pt ;; delta : Account -> Number [1pt] ;; determine the difference between the two lists [1pt] (define (delta a) ;; [2pts: 1 for selectors, 1 for correctness] (- (sum (account-in a)) (sum (account-out a)))) ;; sum : LOAS -> Number [1pt] ;; compute the sum of the numbers on l [1pt] (define (sum l) (cond ;; [3 pts for cond, natural recursion, +] [(empty? l) 0] [else (+ (first l) (sum (rest l)))])) ;; Tests: (equal? ;; [1pt] (sum (cons 9 (cons 10 empty))) 19) (equal? ;; [1pt] (delta (make-account (cons 9 (cons 10 empty)) (cons 3 empty))) 16)