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.
  • (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]))
    


  • (2pts) Transform filter into accumulator style. Here is the code for convenience:
      ;; 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.

  • (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?

  • (2pts) Develop the program split, which consumes a list of numbers and a threshold number. It produces a list of two lists: The function may traverse its input list at most once.

  • (2pts) Develop the program interest. The function interest consumes 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.
    
        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)
         = ...
       |#
    
    Cut and paste the code into DrScheme.