{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

a04sol

# a04sol - A04 Sample Solutions CS135 Fall 2011 Question 2a A...

This preview shows pages 1–8. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ;; A04 Sample Solutions ;; CS135 -- Fall 2011 ;; Question 2a ;; A Balance is a number, b, with at most two decimal places. (define—struct chequing (balance num—cheques)) ;; A Chequing = (make—Chequing Balance Nat) (define—struct line—of—credit (balance credit—limit rate)) ;; A LineOfCredit = (make—line—of—credit Balance Num Num) (define—struct savings (balance type transactions min—balance)) ;; A Savings = (make—savings Balance Symbol Nat Balance) ;; An Account is one of: ;; * Chequing ;; * LineOfCredit * Savings m. a ,, Alternate: \\Eﬂ ;; An Account = (union Chequing LineOfCredit S ving 2/) ;; Question 2b <;> " ;; chequing—fn: Chequing --> Any 1? /f:~i:> ;; (define (chequing—fn chequing) ;; ... (chequing—balance equi g'l.. ;; ... (chequing—num—che ' s che ' g)... ;; loc—fn: LineOfCredit ——> Any ;; (define (loc—fn loc) ;; ... (line—of—credit—balance loc) ;; ... (line—of—credit—credit—limit loc) ;; ... (line—of—credit—rate loc) ;;) ;; savings—fn: Savings ——> Any ;; (define (savings—fn savings) ;; ... (savings—balance savings) ;; ... (savings—type savings) ;; ... (savings—transactions savings) ;; ... (savings—min—balance savings) - Question 2c ;; account—fn: Account ——> Any (define (account—fn account) m. a m. a This document is for the exclusive use of j65lu. ;; (cond [(chequing? account) (chequing—En account)] ;; [(line—of—credit? account) (loc—fn account)] ;; [(savings? account) (savings—fn account)] ;; [else (error "Unrecognized account type.")] ;; J) ;; Question 2d ;; get—balance: Account ——> Balance ;; Produce the balance of the provided account. ; ; Example : (check—expect (get—balance (make—chequing 532.59 5)) 532.59) (define (get—balance account) (cond [(chequing? account) (chequing—balance account)] [(line—of—credit? account) (line—of—credit—balance account)] [(savings? account) (savings—balance account)] [else (error "Unrecognized account type.")] \\\\j> )) \ ;; Tests _// (check—expect , (get—balance (make—line—of—credit 532.5 300 .18,) 532.59) (check—expect (get—balance (make—savings 532.59 :? /{: 'ReguIarInt est" 5 300. 0{; 53 .59) (check—error (get—balance ( e- osn 8) "Unrecognized account type ”) ;; Question 29 (define per—check—fee 0.15) (define max—monthly—savings—transactions 5) ;; avail—funds—chequing: Chequing ——> Balance ;; Purpose: produce the available funds to withdraw from this ;; chequing account (check—expect (avail—funds—chequing (make—chequing 500 2)) 499.55) (check—expect (avail—funds—chequing (make—chequing 0.40 2)) 0) (define (avail—funds—chequing chequing) (max 0 (— (chequing—balance chequing) (* per—check—fee (+ (chequing—num—cheques chequing) 1)))) ) ;; avail—funds—loc: LineOfCredit ——> Balance ;; Purpose: produce the available funds to withdraw from this line of ;; credit account This document is for the exclusive use of j65lu. (check—expect (avail—funds—loc (make—line—of—credit 500 3000 0.18)) 3500) (check—expect (avail—funds—loc (make—line—of—credit —500 3000 0.18)) 2500) (define (avail—funds—loc loc) (+ (line—of—credit—balance loc) (line—of—credit—credit—limit loc))) ;; avail—funds—savings: Savings ——> Balance ;; Purpose: produce the available funds to withdraw from this savings ;; account (check—expect (avail—funds—savings (make—savings 500 ‘HighInterest 5 100)) 0) (check—expect (avail—funds—savings (make—savings 500 ‘HighInterest 3 100)) 500) (define (avail—funds—savings savings) (cond [(and (symbol=? (savings—type savings) ‘HighInterest) (>= (savings—transactions savings) max—monthly—savings—transactions)) 0] [else (savings—balance savings)] \\\\ )) (check—expect (avail—funds—savings (make—savings 500 ‘Regular teres. 33 1’0)) 500) ; shouldn't be possible (too many trans? tion ), b.t check anyway (check—expect (availfunds—savings (make—savings 500 'HighIntanst 100)) 0) Q ;; available—funds: Account 2: Balgn é:> ;; Produce the maximum amount that can» e withdrawn from this account. ;; Examples (check—expect (available—funds (make—L equing 500 2)) 499.55) (check—expect (available—funds (make— equing 0.40 2)) 0) (check—expect (available—funds (makeesavings 500 'HighInterest 5 100)) 0) (check—expect (available—funds (make—savings 500 'HighInterest 3 100)) 500) (check—expect (available—funds (make—line—of—credit 500 3000 0.18)) 3500) (check—expect (available—funds (make—line—of—credit —500 3000 0.18)) 2500) (define (available—funds account) (cond [(chequing? account) (avail—funds—chequing account)] [(line—of—credit? account) (avail—funds—loc account)] [(savings? account) (avail—funds—savings account)] [else (error "Unrecognized account type.”)] )) (check—error (available—funds 100) "Unrecognized account type.”) ;; Question 2f This document is for the exclusive use of j65lu. ;; withdraw—chequing: Chequing Balance ——> Chequing ;; Purpose: Withdraw amount from the account. ; ; Examples : (check—expect (withdraw—chequing (make—chequing 500 2) 250) (make—chequing 250 3)) (check—error (withdraw—chequing (make—chequing 0.40 2) 0.30) "Insufficient funds ”) (define (withdraw—chequing chequing amount) (cond [(<= amount (avail—funds—chequing chequing)) (make—chequing (— (chequing—balance chequing) amount) (+ (chequing—nunrcheques chequing) 1))] [else (error "Insufficient funds II)])) ;; withdraw—loc: LineOfCredit Balance ——> LineOfCredit ;; Purpose: Withdraw amount from the account. ;; Examples: (check—expect (withdraw—loo (make—line—of—credit 500 3000 0.18) 000) (make—line—of—credit —1500 3000 0 18)) (check—expect (withdraw—loo \\\Q .18) _500 (make—line—of—credit 500 3000 (make—line—of—credit —3000 3 00 0 -8)) , (check—error (withdraw—loo -a (make—line—of—credit 500 0 0 0 18) 3500.01) "Insufficient funds."):? ,Jéu (define (withdraw—loo loc amo.nt)<; ' (cond [(<= amount (avail—f dL—loc oc) (make—line—of—credit (— ( ine- f—credit—balance loc) amount) (line- f—credit—credit—limit loc) (line—on—credit—rate loc))] [else (error "Insufficient fu'ds.”)])) ;; withdraw—savings: Savings Balance ——> Savings ;; Purpose: Withdraw amount from the account. ;; Examples: (check—expect (withdraw—savings (make—savings 500 ‘HighInterest 3 300) 400) (make—savings 100 ‘HighInterest 4 100)) (check—expect (withdraw—savings (make—savings 5000 'HighInterest 3 300) 400) (make—savings 4600 ‘HighInterest 4 300)) (define (withdraw—savings savings amount) (cond [(and (symbol=? (savings—type savings) ‘HighInterest) (>= (savings—transactions savings) max—monthly—savings—transactions)) (error "Transaction limit exceeded.”)] This document is for the exclusive use of j65lu. [(<= amount (avail—funds—savings savings)) (make—savings (— (savings—balance savings) amount) (savings—type savings) (+ (savings—transactions savings) 1) (min (— (savings—balance savings) amount) (savings—min—balance savings)))] [else (error "Insufficient funds.”)])) (check—error (withdraw—savings (make—savings 500 ‘HighInterest 5 300) 400) "Transaction limit exceeded.”) (check—error (withdraw—savings (make—savings 500 ‘HighInterest 5 300) 600) "Transaction limit exceeded l') (check—error (withdraw—savings (make—savings 500 ‘HighInterest 3 300) 500.01) "Insufficient funds ") (check—expect (withdraw—savings (make—savings 500 ‘RegularInterest 10 50) 400) (make—savings 100 ‘RegularInterest 11 0)) ;; withdraw: Account Balance ——> Account ;; Purpose: Withdraw amount from the acco nt. \j ;; Examples: .c (check—expect (withdraw (make—savings 50 High nt rest 3 300) 400) (make—savings 100 ‘HighInte£;st 4 100)) (check-expect (withdraw (make—linéiof—c dit!‘ 0 3000 0.18) 2000) (make—line—of—c.edit —'500 .000 0 18)) (check—expect (withdraw (mak —crequin 50 2) 250) (make—chequing 250 _ ) » (define (withdraw account amount) (cond [(or (not (number? amount)) amount 0)) (error "Invalid withdrawal value.")] [(chequing? account) (withdraw—chequing account amount)] [(line—of—credit? account) (withdraw—10c account amount)] [(savings? account) (withdraw—savings account amount)] [else (error "Unrecognized account type.”)] )) (check—error (withdraw (make—posn 3 4) 500) "Unrecognized account type I') - 'k*‘k'k*‘k'k*‘k'k'k'k'k*‘k'k*‘k'k*‘k'k*‘k'k'k'k'k*‘k'k*‘k'k*‘k'k*‘k'k'k'k'k************************ ;; A04 Sample Solutions ;; C8135 -- Fall 2011 ;; Question 3a This document is for the exclusive use of j65lu. ;; A Balance is a number, b, with at most two decimal places. ;; A (listof Balance) is one of: ;; * empty ;; * (cons Balance (listof Balance)) ;; Question 3b ;; balance—list—fn: (listof Balance) ——> Any ;; (define (balance—list—fn lob) ;; (cond [(empty? lob) ...] ;; [else ... (first lob) ;; ... (balance—list—fn (rest lob)) ...])) ;; Question 3c ;; num—overdraws: (listof Balance) ——> Nat ;; Purpose: Count the number of balances that are l s than zero. '; Examples: (check—expect (num—overdraws empty) 0) (check—expect (num—overdraws (cons —5 (cons 3\ (check—expect (num—overdraws (cons 5 (con 3 emp\ ML 5 m. (cond [(empty? lob) 0] \ [(< (first lob) 0) (add1(:num= verdr s (rest lob)))] [else (num—overdraws (res pr}) )) m. (define (num—overdraws lob) H /{/ m. m. (define (num—overdraws lob) .« (cond [(empty? lob) 0] [else (+ (cond [(< (first lob, 0) 1] [else 0]) (num—overdraws (rest lob)))])) ;; Question 3d ;; compute—interest: (listof Balance) Num ——> Num ;; Purpose: sum the interest accumulated each day at the given rate ; ; Examples : (check—expect (compute—interest empty .05) 0) (check—expect (compute—interest (cons 100 empty) .05) (* 1/365 .05 100)) (check—expect (compute—interest (cons 100 (cons 200 empty)) .05) (+ (* 1/365 .05 100) (* 1/365 .05 200))) (define (compute—interest lob rate) (cond [(empty? lob) 0] [else (+ (* 1/365 rate (first lob)) This document is for the exclusive use of j65lu. (compute—interest (rest lob) rate))])) ;; Question 3e I I normalize—amounts: ;; Purpose: (listof Num) ——> (listof Balance) Truncate each value on the list to two decimal places. (check—expect (normalize—amounts (cons 1.00009 (cons 1.99999 empty))) (cons 1.00 (cons 1.99 empty))) (define (normalize—amounts lob) I I ; ; Examples : (check—expect (opening—balanzz (check—expect (opening—balan (define (opening—balance lob) (cond [(empty? (rest lob)) (first 1 b)] [else (opening—balance (rest I I (cond [(empty? lob) empty] [else (cons (/ (floor (* (first lob) 100)) 100) (normalize—amounts (rest lob)))])) Template for non—empty lists nonempty—list: (define (cond Question 3f - opening—balance: (listof Balance) ——> - Purpose: ' (nonemptylistof Balance) (nonempty—list 1st) [(empty? (rest lst)) ...] [else ... (first 1st) (nonempty—list (rest 2 --> Any lane Find the last balanace}bn t “list. cons (c ns 5 empty))) 5) (céhs<: enqty)) 5) 'Ob))])) ;; Question 3g I ' min—balance: ; Examples: (check—expect (min—balance (cons 5 empty)) 5) (check—expect (min—balance (cons 5 (cons 2 empty))) 2) (check—expect (min—balance (cons 2 (cons 5 empty))) 2) (listof Balance) ——> Balance (define (min—balance lob) (cond [(empty? (rest lob)) (first lob)] [(< (first lob) (min—balance (rest lob))) (first lob)] [else (min—balance (rest lob))])) ;; Question 3h This document is for the exclusive use of j65lu. ,; saver?: (listof Balance) ——> Boolean ,; Purpose: determine whether all the balances on lob are ,; non—decreasing. ;; Examples: (check—expect (saver? (cons 5 empty)) true) (check—expect (saver? (cons 5 (cons 4 empty))) true) (check—expect (saver? (cons 4 (cons 5 empty))) false) ;(define (saver? lob) (oond [(empty? (rest lob)) true] [(and (cons? lob) (cons? (rest lob)) (>= (first lob) (first (rest lob)))) (saver? (rest lob))] , [else false])) (check—expect (saver? (cons 5 (cons 5 empty))) true) ; Alternate solution (define (saver? lob) (or (empty? (rest lob)) (and (cons? lob) (cons? (rest lob)) (>= (first lob) (first (rest 10 ))) (saver? (rest lob))))) \/ > > (O \ This document is for the exclusive use of j65lu. ...
View Full Document

{[ snackBarMessage ]}

### Page1 / 8

a04sol - A04 Sample Solutions CS135 Fall 2011 Question 2a A...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online