HtDP Solution Set

Section 3



Problem 1 (Solution):
;tbsp->cup : Number -> Number
(define (tbsp->cup tbsps)
(/ tbsps 16))
 
(tbsp->cup 0)
"should be" 0
(tbsp->cup 16)
"should be" 1
 
;cup->gal : Number -> Number
(define (cup->gal cups)
(/ cups 16))
 
(cup->gal 0)
"should be" 0
(cup->gal 16)
"should be" 1
 
 
;gal->barrel: Number -> Number
(define (gal->barrel gals)
(/ gals 42))
 
(gal->barrel 0)
"should be" 0
(gal->barrel 42)
"should be" 1
 
;tbsp->barrels : Number -> Number
(define (tbsp->barrels tbsps)
(gal->barrel (cup->gal (tbsp->cup tbsps))))
 
(tbsp->barrels 0)
"should be" 0
(tbsp->barrels 10752)
"should be" 1
 


Problem 2 (Solution):
;volume : Number -> Number
; return the volume of a sphere given its radius
(define (volume r)
(/ (* 4 pi (expt r 3)) 3))
 
(volume 0)
"should be" 0
(volume 1)
"should be" (* 4/3 pi)
(volume 2)
"should be" (* 4/3 pi 8)
 
;shell-volume: Number Number -> Number
(define (shell-volume r1 r2)
(- (volume r2) (volume r1)))
 
(shell-volume 0 0)
"should be" 0
(shell-volume 0 1)
"should be" (* 4/3 pi)
(shell-volume 1 2)
"should be" (* 4/3 pi 7)
 


Problem 3 (Solution):
;fed1: Number -> Number
; given an income of $35,000 or below, return tax
(define (fed1 inc)
(* inc .15))
 
;fed2: Number -> Number
; given an income between $35,000 and $100,000 return tax,
; only for portion greater than $35,000
(define (fed2 inc)
(* (- inc 35000) .25))
 
 
;fed3: Number -> Number
; given an income over $100,000 return tax, only for portion
; greater than $100,000
(define (fed3 inc)
(* (- inc 100000) .35))
 
;state1: Number -> Number
; given an income between 0 and $50,000 return 0
(define (state1 inc)
0)
 
;state2: Number -> Number
; given an income over $50,000, return state tax amount
(define (state2 inc)
(* (- inc 50000) .05))
 
(define (tax-for-over-100000 inc)
(+ (fed1 35000)
(fed2 100000)
(fed3 inc)
(state1 50000)
(state2 inc)))
 
(tax-for-over-100000 100001)
"should be" (+ (* .15 35000) (* .25 65000) (* .35 1) (* .05 50001))
(tax-for-over-100000 200000)
"should be" (+ (* .15 35000) (* .25 65000) (* .35 100000) (* .05 150000))
 
;What must change?
; - only the multiplier in the state2 function
 


Problem 4 (Solution):
;; collision-speed : Num Num Num Num -> Num
(define (collision-speed m1 v1 m2 v2)
(/ (+ (momentum m1 v1) (momentum m2 v2))
(+ m1 m2)))
 
;; momentum : Num Num -> Num
;; to compute the momentum from the veolicity and mass of an object
(define (momentum m v)
(* m v))
 
(momentum 10 -3)
"should be" -30
(collision-speed 10 3 10 -3)
"should be" 0
(collision-speed 10 4 5 -20)
"should be" -4
 


Problem 5 (Solution):
  (* 0.08 30 principal))
 
(define (compute-principal sell down)
(- sell down))
 
(define (total-cost sell down)
(+ sell (compute-interest (compute-principal sell down)) 1000))
 
(compute-principal 100000 10000)
"should be" 90000
(compute-interest 100000)
"should be" 240000
(total-cost 110000 10000)
"should be" 351000
 
 



Jamie Raymond
Matthias Felleisen
 

23 september 2002