a04sol

# a04sol - Assignment 04 Solution Winter12 ;; Question 2 ; a...

This preview shows pages 1–6. 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 is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Assignment 04 Solution Winter12 ;; Question 2 ; a ;; count-positives : [listof Num] -> Nat ;; Consumes a [listof Num] and produces a count of ;; all of the positive numbers in the list. ;; Examples: [check-expect [count-positives empty] 0] [check-expect [count-positives [cons 1 empty]] 1] [check-expect [count-positives [cons -1 empty]] 0] [define [count-positives alon] [cond - [[empty? alon] 0] \ ' [[positive? [first alon]] [+ 1 [count-positives [rest alon ]] [else [count-positives [rest alon]]]]] -- [check-expect [count-positives [cons 1 [com -1 [QQXZ/lmptyDD 2] [check-expect [count-positives [cons 0 erupt ] 0] I ‘N. :b ;; some-negative? : [listof Numfﬁ boolé‘aﬁ ;; Consumes a [listof Num] a(1_d p10d ces tr e ifand only if ;; at least one number in the list 8 neg. tive. ;; Examples: \ [check-expect [some-nega ive? empty] false] [check-expect [some-negative?/[cons 1 empty]] false] [check-expect [some-negaﬁve? [cons -1 empty]] true] [define [some-negative? alon] [cond [[empty? alon] false] [[negative? [ﬁrst alon]] true] [else [some-negative? [rest alon]]]]] [check-expect [some-negative? [cons 1 [cons -1 empty]]] true] [check-expect [some-negative? [cons 0 empty]] false] ;c ;; reciprocals: [listof Num] -> [listof [union Symbol Num]] ; ; Consumes a [listof Num] and produces the list resulting from transforming each number in the This document is for the exclusive use of x8wen. ;; list into its reciprocal. Every occurrence of 0 in the original list should be transformed to ’error. ;; Examples: [check-expect [reciprocals [cons 0 empty]] [cons 'error empty]] [check-expect [reciprocals [cons 1 [cons 2 [cons 3 empty]]]] [cons 1 [cons 0.5 [cons 1/3 empty]]]] [define [reciprocals alon] [cond [[empty? alon] empty] [[= [first alon] 0] [cons 'error [reciprocals [rest alon]]]] [else [cons [/ 1 [first alonj] [reciprocals [rest alon]]]]]] [check-expect [reciprocals empty] empty] . \ [check-expect [reciprocals [cons 0 [cons 2 empty]]] [cons 'error [cons 0.5 empty]]] '-~. id </ ;; differences: [listof Num] -> [listof Num] - ; ; Consumes a non-empty list of Num, aﬁdproc 1ces the list of differences between x. ».. consecutive entries. ;; Examples: "Ru. [check-expect [differences [€011s\1 _ cons 4 cons 5 empty]]]] [cons 5 [cons 1 empty]]] - [check-expect [differencei [con 5 e1 ty]] empty] [define [differences alon] [cond / [[empty? [rest alonj] em ty] [else [cons [- [first [rest alon]] [first alon]] [differences [rest alon]]]]]] [check-expect [differences [cons 5 [cons 4 [cons -1 [cons -2 empty]]]]] [cons -1 [cons -5 [cons -1 empty]]]] ;e ;; distance: Posn Posn -> Num ;; Consumes two posns and produces the distance between them ;; Example: [check-within [distance [make-posn 0 0] [make-posn 1 1]] [sqrt 2] 0.00000000001] [define [distance pos1 pos2] [sqrt [+ [sqr [- [posn-y pos 2] [posn-y pos1]]] [sqr [- [posn-x pos 2] [posn-x pos1]]]]]] This document is for the exclusive use of x8wen. [check-within [distance [make-p051] 0 0] [make-p051] -3 4]] 5 0.001] [check-within [distance [make-p051] 1 1] [make-p051] -3 4]] 6.40312 0.001] ;; min-distance: [list0f P0511] P0511 -> P0511 ;; consumes a non-empty list of P05ns alop and a P0511 p05. [min-distance alop p05] produces ;; the smallest distance be- tween p05 and one of the points in alop. ;; Example: [check-expect [min-distance [list [make-p051] 3 4] [make-p051] 6 8]] [make-p051] 0 0]] 5] [define [min-distance alop p05] . I [c0nd \ [[empty? [rest alopj] [distance [first alop] 1305]] [else [min [distance [first alop] p05] [min-distance [rest alop] p05]]]]] ( ) [check-expect [min-distance [list [make-p051] 0 0] “ [make-p051] 6 8]] Pat... [make-p051100]] d . 0] > [check-expect [min-distan\ce / [list [make-p051] 3 4 J] [make-p051] 0 0]] 5] [check-expect [min-distance [list [make-p051] 6 8] [make-p051] 3 4]] [make-p051] 0 0]] 5] [check-within [min-distance [list [make-p051] 2 2] [make-p051] 7 8]] [make-p051] 1 1]] [sqrt 2] 0.000000001] This document is for the exclusive use of x8wen. ;;Question 3 ;a ;; A Balance is a number, b, with at most two decimal places. ;; A [listof Balance] is one of: ;; * empty ;; * [cons Balance [listof Balance]] :1) ;; balance-list-fn: [listof Balance] -> Any ;; [define [balance-list-fn lob] ;; [cond [[empty? lob] ...] ;; [else [first lob] ;; [balance-list-fn [rest lob]] ...]]] . \ ;c ;; num-overdraws: [listof Balance] -> Nat ;; Purpose: Count the number ofbalances tl at aiél sjmn zero. ;; Examples: [check-expect [num-overdraws [cons 5 [eons .'_ empty]]] 0] [define [num-overdraws lob] a: [cond [[empty? lob] 0] N . [[< [first lob] 0] [add1 [lHlIKOVﬂI‘aWS [rest lob]]]] J. J [else [num-overdraw:\ [res ob] [check-expect [num-overdraws empty] 0] [check-expect [num-overcﬂawﬁcons -5 [cons 3 [cons -2 empty]]]] 2] :d ;; compute-daily-interest: [listof Balance] Num -> Num ; ; Purpose: sum the interest accumulated each day at the given rate ;; Examples: [check-expect [compute-daily-interest [cons 100 [cons 200 empty]] .05] [+ [* 1/365 .05 100] [* 1/365 .05 200]]] [define [compute-daily-interest lob rate] [cond [[empty? lob] 0] [else [+ [* 1/365 rate [first lob]] [compute-daily-interest [rest lob] rate]]]]] [check-expect [compute-daily-interest empty .0 5] 0] [check-expect [compute-daily-interest [cons 100 empty] .05] [* 1/365 .05 100]] This document is for the exclusive use of x8wen. ; ; Template for non-empty lists ;; nonempty-list: [listof Balance] [nonempty] -> Any ; ; [define [nonempty-list lst] ;; [cond [[empty? [rest lst]] [first lst]...] ;; [else [first lst] [nonempty-list [rest lst]] ...]]] ;e ;; month-end: [listof Balance] -> [listof Balance] ;; Purpose: consumes a non-empty [listof Balance], and produces the [listof Balance] resulting ; ; from adding the interest earned to the final daily balance [i.e., the first entry] ;; of the given list [the rest of the given list should be return d unchanged]. ;; Examples: . [check-expect [month-end [cons 100 empty] 0.05] [co\n [/ [ﬂoo [* 100 [+ 100 [* 1/365 .05 100]]]]100]empty]] .‘H [check-expect [month-end [cons 100 [cons 200 e npty ] .05 [cons [/ [floor [* 100 [+ 100 [* 1/36 .05 10 [* 1/365 .05 200]]]]100] [cons 200 empty]]] - xx [define [month-end lob rate] [cons [/ [floor [* 100 NEWK [+ [compute-dailfingrest lob r e] [first lob]]]] ' - 100) \ C I. [rest lob]]] [check-expect [month-end cor£50 [cons 100 [cons 200 empty]]] .05] [cons [/ [floor [* 10 [+ 50 [* 1/365 .05 50] [* 1/365 .05 100] [* 1/365 .05 200]]]] 100] [cons 100 [cons 200 empty]]]] :f ;; opening-balance: [listof Balance] -> Balance ;; Purpose: Find the last balanace 011 the list. ;; Examples: [check-expect [opening-balance [cons 5 empty]] 5] [check-expect [opening-balance [cons 2 [cons 5 empty]]] 5] [define [opening-balance lob] [cond [[empty? [rest lobj] [first lobj] [else [opening-balance [rest lob]]]]] [check-expect [opening-balance [cons 1 [cons 2 [cons 3 [cons 4 empty]]]]] 4] This document is for the exclusive use of x8wen. :g ;; suspicious?: [listof Balance] -> Boolean ;; Purpose: Consume a list ofbalance and produce true if there are any ;; sudden drops forom one balance to the next over 1000 and false otherwise ;; Examples: [check-expect (suspicious? [cons 50 [cons 1500 empty]]] true] [define (suspicious? lob] [cond [[empty? [rest lob]] false] [[< 1000 [- [first [rest lob]] [first lob]]] true] [else [suspicious? [rest lob]]]]] [check-expect [suspicious? [cons 100 empty]] false] . x [check-expect [suspicious? [cons 50 [cons 1050 emptyj} false] [check-expect [suspicious? [cons 100 [cons 500 [cons 50 ons 500 [cons 3000 [cons 500 empty]]]]]]] true] This document is for the exclusive use of x8wen. ...
View Full Document

## This note was uploaded on 04/02/2012 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.

### Page1 / 6

a04sol - Assignment 04 Solution Winter12 ;; Question 2 ; a...

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

View Full Document
Ask a homework question - tutors are online