Assignment 9: Accumulators

###
Assignment 9: Accumulators

Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks, and Grading Guidelines. Above all, keep your work neat and honest.

Notes: The first three problems ask for an accumulator transformation. The fact that the program employs accumulator-style should not be visible to the user. If you simplify the program, show the intermediate steps.
1. (2pts) Transform `add` into accumulator style:

         ;; add : natural-number natural-number -> natural-number
         (define (add n m)
           (cond
             [(positive? n) (add1 (add (sub1 n) m))]
             [else ;  (zero? n)
               m]))

2. (2pts) Transform `filter` into accumulator style. Here is its definition:

         ;; filter : (X -> bool) (listof X) -> (listof X)
         (define (filter pred? alist)
           (cond
             [(cons? alist)
              (if (pred? (first alist))
       	   (cons (first alist) (filter pred? (rest alist)))
       	    (filter pred? (rest alist)))]
             [else ; (empty? alist)
       	empty]))

   Cut and paste the code into DrScheme.
3. (2pts) Evaluate

         (filter (lambda (x) (> x 0)) (list 1 -1 2 -2))

   by hand for both versions of `filter`.

   How do the two programs differ? When is it acceptable to replace `filter` with its accumulator version and when not?

4. (2pts) Develop the program `split`, which consumes a list of numbers and a threshold number. It produces a list of two lists:
   - those numbers in the first argument that are greater than the threshold
   - those numbers in the first argument that are less than the threshold
   The function may traverse its input list at most once.
5. (2pts) Develop the program `interest`. The function interest consumes
   - a base amount
   - a list of transactions
   - a last day
   The last day is guaranteed to be larger than the day component of the last item on the transactions.

   The function computes the interest that the account earned or lost in the period. The computation is non-accumulative, that is, the interest is not added during the computation period but at its end. The daily interest rate is `(/ 5/100 360)`. While the account is less than 0, the account is charged a loan rate; the loan rate is `(/ 8/100 360)`.

   Here is the beginning of the program:

          #|
          Data Definitions:

           A day is a positive integer.

           An amount is a number:
            a positive number represents a deposit;
            a negative number represents a withdrawal.
            an amount of zero is acceptable and can be treated as either a deposit
               or a withdrawal

           A transaction is (list day amount).

          Contract:
           interest : amount (listof transaction) day -> num

          Program Examples:

          1.
              (interest 1000 empty 30)
            = (* 1000 30 DAILY-INTEREST-RATE)
            = ...

          2.
              (interest 0 (list (list 10 1000)) 30)
            = (+ (* 0 (- 10 0)  DAILY-INTEREST-RATE)
       	  (* 1000 (- 30 10) DAILY-INTEREST-RATE))
            = (* 1000 (- 30 10) DAILY-INTEREST-RATE)
            = ...
          |#

Matthias Felleisen | This page was generated on Fri Mar 5 09:05:54 CST 1999.