HtDP Solution Set | Section 6 |
Problem 1 (Solution): ;area-of-circle: Posn Posn -> Number
; - given the center and point on circumference, return the area
; enclosed by circle
(define (area-of-circle c p)
(* pi (sqr (seg-length c p))))
;seg-length: Posn Posn -> Number
; - compute distance between two points
(define (seg-length p1 p2)
(sqrt (+ (sqr (- (posn-x p1) (posn-x p2)))
(sqr (- (posn-y p1) (posn-y p2))))))
#| Tests |#
(seg-length (make-posn 0 0) (make-posn 1 1))
"should be" (sqrt 2)
(seg-length (make-posn -3 -2) (make-posn 0 2))
"should be" 5
(area-of-circle (make-posn 0 0) (make-posn 1 1))
"should be" (* 2 pi)
(area-of-circle (make-posn 1 2) (make-posn 3 4))
"should be" (* 8 pi)
Problem 2 (Solution): ; A Paycheck is a structure
; (make-paycheck Number Symbol Symbol Number)
(define-struct paycheck (cnum name date amount))
;create-paycheck: Number Symbol Symbol Number Number Number -> Paycheck
(define (create-paycheck cnum name date hours wage taxrate)
(make-paycheck cnum name date
(* hours wage (- 1.00 taxrate))))
#| Tests |#
(create-paycheck 1001 'Allen 'Feb-5-2002 40 16 .25)
"should be" (make-paycheck 1001 'Allen 'Feb-5-2002 480.0)
Problem 3 (Solution): ;A Score is structure
; (make-score Symbol Number)
(define-struct score (name points))
;outcome: Score Score -> Symbol
; - given the scores from a game,
; return the name of the winner or 'Tie
(define (outcome s1 s2)
(cond
[(> (score-points s1) (score-points s2))
(score-name s1)]
[(< (score-points s1) (score-points s2))
(score-name s2)]
[else 'Tie]))
#| Tests |#
(outcome (make-score 'Bears 20) (make-score 'Eagles 30))
"should be" 'Eagles
(outcome (make-score 'Raven 10) (make-score 'Cubs 10))
"should be" 'Tie
(outcome (make-score 'Jen 8) (make-score 'April 5))
"should be" 'Jen
Problem 4 (Solution): ;An Entry is a structure
; (make-entry Number Symbol Number Symbol)
(define-struct entry (item bidder amount status))
;new-bid: Symbol Number Entry -> Entry
; - return the resulting entry of a new auction bid
(define (new-bid a-bidder a-bid an-entry)
(cond
[(symbol=? (entry-status an-entry) 'Closed) an-entry]
[(< a-bid (entry-amount an-entry)) an-entry]
[else
(make-entry (entry-item an-entry)
a-bidder
a-bid
(entry-status an-entry))]))
#| Tests |#
(new-bid 'Bbidder 60 (make-entry 4004 'Abidder 50 'Open))
"should be" (make-entry 4004 'Bbidder 60 'Open)
(new-bid 'Bbidder 60 (make-entry 4004 'Abidder 50 'Closed))
"should be" (make-entry 4004 'Abidder 50 'Closed)
(new-bid 'Bbidder 40 (make-entry 4004 'Abidder 50 'Closed))
"should be" (make-entry 4004 'Abidder 50 'Closed)
Problem 5 (Solution): ;An Icecube is a structure
; (make-icecube Number Number)
(define-struct Icecube (mass velocity))
;collision-result: Icecube Icecube -> Icecube
; return Icecube after collision
(define (collision-result sc1 sc2)
(make-icecube
(+ (icecube-mass sc1)
(icecube-mass sc2))
(/ (+ (momentum sc1) (momentum sc2))
(+ (icecube-mass sc1)
(icecube-mass sc2)))))
;momentum: Icecube -> Number
; calculate the momentum of a cube
(define (momentum sc)
(* (icecube-mass sc)
(icecube-velocity sc)))
#| Tests |#
(momentum (make-icecube 2 3))
"should be" 6
(collision-result (make-icecube 3 3) (make-icecube 3 -3))
"should be" (make-icecube 6 0)
(collision-result (make-icecube 2 4) (make-icecube 8 -2))
"should be" (make-icecube 10 -8/10)
Problem 6 (Solution): ; Symbol -> true
(define (draw-random-baloon go)
(draw-baloon (random WIDTH) (random HEIGHT) (pick-color (random 4))))
; Num Num Symbol[color] -> true
; to draw a color-ed baloon, centered at (make-posn x y)
(define (draw-baloon x y color)
(and (draw-solid-disk (make-posn x y) RADIUS color)
(draw-solid-line (make-posn x (+ RADIUS y))
(make-posn x (+ RADIUS y LENGTH)))))
; Num -> Symbol
; to translate a number in [0,100] to a color
(define (pick-color r)
(cond
[(= r 0) 'red]
[(= r 1) 'blue]
[(= r 2) 'green]
[(= r 3) 'yellow]))
(define WIDTH 300)
(define HEIGHT 300)
(define RADIUS 20)
(define LENGTH 40)
#| Tests |#
(symbol=? (pick-color 0) 'red)
(symbol=? (pick-color 1) 'blue)
(symbol=? (pick-color 2) 'green)
(symbol=? (pick-color 3) 'yellow)
;; --- drawing baloons at known places
(start WIDTH HEIGHT)
(draw-baloon 100 10 'green)
(draw-baloon 200 10 'blue)
(sleep-for-a-while 2)
;; --- ... and at random places
(draw-random-baloon 'go)
(draw-random-baloon 'go)
(draw-random-baloon 'go)
(draw-random-baloon 'go)
(draw-random-baloon 'go)
(draw-random-baloon 'go)
(draw-random-baloon 'go)
Problem 7 (Solution): ; Num Num Num Num -> true
; draw four bars corresponding to the heights h1, h2, h3, h4
; assume: 0 <= h1, h2, h3, h4 <= HEIGHT - 20
(define (draw-bar-chart h1 h2 h3 h4)
(and
(draw-solid-line (make-posn 0 BASE-LINE) (make-posn WIDTH
BASE-LINE))
(draw-solid-rect (make-posn X1 (- BASE-LINE h1)) RWIDE h1 'red)
(draw-solid-rect (make-posn X2 (- BASE-LINE h2)) RWIDE h2
'blue)
(draw-solid-rect (make-posn X3 (- BASE-LINE h3)) RWIDE h3
'green)
(draw-solid-rect (make-posn X4 (- BASE-LINE h4)) RWIDE
h4 'yellow)
))
(define WIDTH 220)
(define HEIGHT 120)
(define BASE-LINE (- HEIGHT 20))
(define RWIDE (* 1/4 (- WIDTH 20)))
(define X1 10)
(define X2 (+ X1 RWIDE))
(define X3 (+ X2 RWIDE))
(define X4 (+ X3 RWIDE))
#| Drawings ... |#
(start WIDTH HEIGHT)
(draw-bar-chart 25 85 30 10)