The bank will charge you for every check you write, but this amount that is charged is based on your balance. If your balance is less than $100, each check you write will cost 50 cents. If your balance is between $101 and $200, the bank will charge you 40 cents per check. If your balance is between $201 and $500, the check charge is only 30 cents. If you keep a $500 balance, then there is no charge for writing checks.In reading the problem, we realize that there are several cases to be considered. The data analysis step of the design recipe shows four cases:Write a function which takes a balance (in dollars) and returns the per-check charge.
Now we'll write a contract, examples, header, and template for the function:
; check-charge : num (balance) => num (per-check charge) ; Ex: (check-charge 0) => 0.50 ; Ex: (check-charge 100) => 0.50 ; Ex: (check-charge 100.01) => 0.40 ; Ex: (check-charge 200) => 0.40 ; Ex: (check-charge 350) => 0.30 ; Ex: (check-charge 500) => 0 ; Ex: (check-charge 600) => 0 (define (check-charge balance) (cond [ question answer ] [ question answer ] [ question answer ] [ question answer ]))Note that we listed an example for each case, as well as each of the "borderlines", a total of seven examples.
Next we fill in the questions:
(define (check-charge balance) (cond [(< balance 100) answer ] [(and (>= balance 100) (< balance 200)) answer ] [(and (>= balance 200) (< balance 500)) answer ] [(>= balance 500) answer ]))Since the second case won't even be looked at if the first case works, and the third case won't be looked at if the first or second case works, we can rewrite this more briefly as
(define (check-charge balance) (cond [(< balance 100) answer ] [(< balance 200) answer ] [(< balance 500) answer ] [(>= balance 500) answer ]))We then fill in the answers:
(define (check-charge balance) (cond [(< balance 100) 0.50] [(< balance 200) 0.40] [(< balance 500) 0.30] [(>= balance 500) 0]))We now test this on each of the seven examples (which I'll leave to you) and confirm that it works.