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