HtDP Solution Set

Section 4



Problem 1 (Solution):
;in-circle?: num num num -> boolean
; - determine whether point is in circle
(define (in-circle? x y r)
(<= (+ (sqr x) (sqr y)) (sqr r)))
 
 
#| Tests |#
(in-circle? 0 0 0)
"should be" true
(in-circle? 1 1 1)
"should be" false
(in-circle? 1 1 2)
"should be" true
 


Problem 2 (Solution):
;;new-price: Number Number -> Number
;; - produce discounted price based on weeks
(define (new-price price weeks)
(cond
[(< weeks 2) price]
[(and (>= weeks 2) (< weeks 3)) (* price .75)]
[(and (>= weeks 3) (< weeks 4)) (* price .50)]
[else (* price .25)]))
 
 
#| Tests |#
(new-price 1 0)
"should be" 1
(new-price 1 2)
"should be" .75
(new-price 1 3)
"should be" .5
(new-price 1 5)
"should be" .25
 
 


Problem 3 (Solution):
;;pieces : Number Number -> Number
;; - given hour of day, compute pieces produced
(define (pieces hour workers)
(cond
[(and (>= hour 6) (< hour 10))
(* workers 30)]
[(and (>= hour 10) (< hour 14))
(* workers 40)]
[(and (>= hour 14) (< hour 18))
(* workers 35)]))
 
(pieces 6 10)
"should be" 300
(pieces 10 10)
"should be" 400
(pieces 14 10)
"should be" 350
 


Problem 4 (Solution):
;;charge : Number Number -> Number
;; - given MB data transferred and base base, compute charges
(define (charge data base)
(cond
[(<= data 100) (* data base)]
[(and (> data 100) (<= data 500))
(* (+ (* base 1.33) .05) data)]
[(and (> data 500) (<= data 1500))
(* (+ (* base 1.44) .08) data)]
[(> data 1500)
(* (* base 2) data)]))
 
(charge 100 1)
"should be" 100
(charge 501 1)
"should be" 761.52
(charge 1501 1)
"should be" 3002
 


Problem 5 (Solution):
;;tax: Number -> Number
;; - return tax given income
(define (tax income)
(+ (fed-tax income) (state-tax income)))
 
;;fed-tex: Number -> Number
;; - return federal tax given income
(define (fed-tax income)
(cond
[(<= income 35000) (* income .15)]
[(and (> income 35000) (<= income 100000))
(+ (* 35000 .15) (* (- income 35000) .25))]
[(> income 100000)
(+ (* 35000 .15) (* (- 100000 35000) .25) (* (- income 100000) .35))]))
 
 
;;state-tax: Number -> Number
;; - return state tax given income
(define (state-tax income)
(cond
[(<= income 50000) 0]
[(> income 50000) (* .05 (- income 50000))]))
 
(fed-tax 35000)
"should be" 5250
(fed-tax 100000)
"should be" 21500
(fed-tax 200000)
"should be" 56500
 
(state-tax 50000)
"should be" 0
(state-tax 50001)
"should be" 0.05
 
(tax 35000)
"should be" 5250
(tax 75000)
"should be" 16500
(tax 100000)
"should be" 24000
 



Jamie Raymond
Matthias Felleisen
 

23 september 2002