This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: ;; Question 2: ;; Part (a) (define—struct Line (slope intercept)) ;; A Line = (make—Line (union Num Symbol) Num) ;; Part (b)
;; helper function
;; compute—slope: Posn Posn —> Num
;; Purpose:
;; compute the slope between two points, assuming it is defined
;; Examples:
(check—expect (compute—slope (make—posn 0 0) (make—posn 3 3)) 1)
(check—expect (compute—slope (make—posn 3 4) (make—posn 5 7)) 3/2)
(define (compute—slope p1 p2)
(/ ( (posny p1) (posny p2)) ( (posnx p1) (posnx p2))))
;; Tests:
(check—expect (compute—slope (make—posn —2 4) (make—posn 3 —7)) —11/5)
(check—expect (compute—slope (make—posn 7 6) (make—posn 9 6)) 0) ;; two—points—>Line: Num Num —> Line
; Purpose: create a Line structure u ‘1 (i.e., calculate slope and intercept)
;; based on the given points (Posn's)
;; if the line is non—vertical, the intjécept is the , a.
c ;; y—intercept. If the line is vertical, ;; is the x—intercept ;; Examples: ;? //: (check—expect (‘ ”' ﬂ
(two—points—>Line (make—posn>p 0) (Gake—losn —2 —2))
(make—Line 1 0)) (check—expect 
(two—points—>Line (make—posn 3 4) ( e—posn 5 5))
(make—Line 1/2 5/2)) (define (two—points—>Line p1 p2) (cond
;; check for vertical line
[(= (posn—x p1) (posn—x p2)) (make—Line ‘undefined (posn—x pl))]
;; non—vertical
[else (make—Line
(compute—slope p1 p2)
( (posny p1) (* (computeslope P1 p2) (posnx p1))))])) ;; Tests: ;; test vertical (check—expect
(two—points—>Line (make—posn 4 3) (make—posn 4 7))
(make—Line ‘undefined 4)) ;; test negative slope (check—expect
(two—points—>Line (make—posn —2 4) (make—posn 3 —7))
(make—Line —11/5 —2/5)) This document is for the exclusive use of j65lu. ;; test horizontal (check—expect
(two—points—>Line (make—posn 7 6) (make—poem 9 6))
(make—Line 0 6)) ;; Part (c)
;; perp—Line: Posn Line —> Line
;; Purpose:
;; Produce a new Line which is perpendicular (i.e., at 90 degrees)
;; to the given Line, which passes through the given point
;; Examples:
(check—expect (perp—Line (make—posn 3 2) (make—Line l 0))
(make—Line —1 5))
;; Definition
(define (perp—Line point line)
(cond
[(equal? (Line—slope line) 'undefined)
(make—Line 0 (posn—y point))]
[(= (Line—slope line) 0) (make—Line 'undefined (posn—x point))]
[else
(make—Line (— (/ l (Line—slope line))) \\\\{ (— (posn—y point) (* (posn—x p int) —/} l (Line—slope
line))))))])) ;; Tests a
(check—expect (perp—Line (make—posn 3 4) f e— i 'undefined 6)) (make—Line o 4)) /‘ (check—expect (perp—Line (make—posn 7 8 (mag Line 0 4))
(make—Line 'un \fine (3)
(check—expect (perp—Line (ma —}osn b 9
(make—Line 5/11 69/+l))
(check—expect (perp—Line (make—posn 0 0) (make—Line 'undefined 0))
(make—Line 0 0)) ) (make—Line —11/5 —2/5)) This document is for the exclusive use of j65lu. ;; Question 3 ;; Part (a) (define—struot Card (suit rank))
;; A Card=(make—Card Symbol Num)  Part (b) ;; better—Card: Card Card —> Card ;; Purpose: ;; Given two cards return the card either with higher suit value
;; or, if they are the same suit, the higher card value a.
d ;; Examples:
(check—expect (better—Card (make—Card ‘spades l) (make—Card ‘diamonds 13))
(make—Card 'spades 1)) ;; Tests Case 3
(check—expect (better—Card (make—Card ‘olubs 3) (make—Card ‘olubs 4))
(make—Card 'olubs 4)) ;; Tests Case 2
(define (better—Card o1 o2)
(oond ;; if the suits are the same
[(symbol=? (Card—suit o1) (Card—suit o2)) (oond
[(> (Card—rank o1) (Card—rank o2)) cl] ;; Case 1
[else o2])] ;$\Case L
;; if the suits are different _ Cas 3 [(symbol=? (Card—suit o1) ‘spades) o1) [(symbol=? (Card—suit o2) ‘spades) r'] a. ;; Case 4 [(symbol=? (Card—suit o1) ‘hearts) o ] ,; Case 5 [(symbol=? (Card—suit o2) ‘hearts) aziiu ,; Case 6 [(symbol=? (Card—suit o1) ‘diﬁmonds o1]!, ,; Case 7
' ,; Case 8 [ l 21)) e2? ° > K ;; test Case 1 (check—expect (better—Card (make—Cai ‘hearts 3) (make—Card ‘hearts 2))
(make—Card 'hearts 3)) ;; test Case 3 (check—expect (better—Card (make—Card ‘spades l) (make—Card ‘hearts 13))
(make—Card 'spades 1)) ;; test Case 4 (check—expect (better—Card (make—Card ‘olubs l3) (make—Card 'spades 1))
(make—Card 'spades 1)) ;; test Case 5 (check—expect (better—Card (make—Card ‘hearts 3) (make—Card ‘diamonds 13))
(make—Card 'hearts 3)) ;; test Case 6 (check—expect (better—Card (make—Card ‘diamonds 13) (make—Card ‘hearts 3))
(make—Card 'hearts 3)) ;; test Case 7 (check—expect (better—Card (make—Card ‘diamonds 13) (make—Card ‘olubs 1))
(make—Card 'diamonds 13)) ;; test Case 8 (check—expect (better—Card (make—Card ‘olubs 1) (make—Card ‘diamonds 13))
(make—Card 'diamonds 13)) This document is for the exclusive use of j65lu. ;; Part (c) ;; Define some cards for testing various functions ;; Define some cards (as constants) to make examples/testing easier
(define cardl (make—Card 'spades 3)) (define card2 (make—Card 'spades 4)) (define card3 (make—Card 'spades 5)) (define card4 (make—Card 'diamonds 3)) (define card5 (make—Card 'clubs 3)) ;; helpers:
;; same—suit?: Card Card Card —> Boolean
;; Purpose:
;; Given three cards, return true if and only if they
;; are the same suit
;; Examples:
(check—expect (same—suit? cardl card2 card3) true)
(check—expect (same—suit? cardl card3 card4) false)
(define (same—suit? c1 c2 c3) (and (symbol=? (Card—suit c1) (Card—suit c2)) (s
03)”)
;; Tests:
;; Try all other positions of one non suited c rd\\\\_//
(check—expect (same—suit? cardl card4 ca 3) fal.e)
(check—expect (same—suit? card4 card3 ca 1) .alse
;; Try with three different suits
(check—expect (same—suit? cardl cardﬁ cardéu fa se) 1:? (Cardsuit c2) (Cardsuit ;; all—same—num?: Num Num Num>f> gooeean ;; Purpose: ;; Returns true if all three numb: s ar the same, false otherwise
'; Examples: (define (all—same—num? numl num2 num3)
(and (= numl num2) (= numl num3))) ;; Tests: (check—expect (all—same—num? 3 4 3) false) (check—expect (all—same—num? 4 3 3) false) (check—expect (all—same—num? 2 3 4) false) ;; consecutive—num?: Num Num Num —> Boolean
;; Purpose:
;; Returns true if all three numbers are consecutive, false otherwise
;; Examples:
(check—expect (consecutive—num? 6 7 8) true)
(check—expect (consecutive—num? 2 4 6) false)
;; Definition
(define (consecutive—num? numl num2 num3) (and (= (— (max numl num2 num3) (min numl num2 num3)) 2) (= (— (max numl num2 num3) 1) (/ (+ numl num2 num3) 3)))) This document is for the exclusive use of j65lu. ;; Tests: (check—expect (consecutive—mum? 1 2 3) true)
(check—expect (consecutive—num? 1 3 2) true)
(check—expect (consecutive—num? 2 1 3) true)
(check—expect (consecutive—num? 2 3 1) true)
(check—expect (consecutive—num? 3 2 1) true)
(check—expect (consecutive—num? 3 1 2) true)
(check—expect (consecutive—num? 3 4 6) false)
(check—expect (consecutive—num? 3 5 6) false)
(check—expect (consecutive—num? 6 3 4) false) ;; two—same—num? Num Num Num —> Boolean ;; Purpose: ;; Produce true if (at least) two of the numbers are the same
;; and false otherwise ;; Examples: (check—expect (two—same—nmn? 1 1 3) true) (check—expect (two—same—num? 1 2 3) false) ;; Definition
(define (two—same—num? numl num2 num3)
(or (= numl num2) (= numl num3) (= num2 num3)))
;; Tests: \\\:\\\é>
(check—expect (two—same—nmn? 1 1 1) true) _ j (check—expect (two—same—nmn? 1 3 1) true)
(check—expect (two—same—num? 3 1 1) true' ;; Purpose: _
;; Given 3 cards, output the Zest lag: va.ue
;; straight—flush, flush, str ight, t ree of—a—kind, pair, high—card
;; Examples: o
(check—expect (hand—value 0ardl care card3) ‘straight—flush)
(check—expect (hand—value 0ardl 0ard4 card5) ‘three—of—a—kind)
;; Definition
(define (hand—value 01 02 03)
(00nd
[(and (same—suit? 01 02 03) (consecutive—num? (Card—rank 01) (Card—rank 02)
(Card—rank 03))) ‘straight—flush]
[(same—suit? 01 02 03) ‘flush]
[(0onse0utive—num? (Card—rank 01) (Card—rank 02) (Card—rank 03)) ‘straight]
[(all—same—num? (Card—rank 01) (Card—rank 02) (Card—rank 03)) ‘three—of—a—kind]
[(two—same—num? (Card—rank 01) (Card—rank 02) (Card—rank 03)) ‘pair]
[else 'high—0ard]))
;; Tests
(check—expect (hand—value 0ardl 0ard2 (make—Card 'spades 13)) ‘flush)
(check—expect (hand—value 0ardl 0ard4 card3) ‘pair)
(check—expect (hand—value 0ard5 0ard3 0ard2) ‘straight)
(check—expect (hand—value 0ard1 0ard2 (make—Card '0lubs 13)) 'high—0ard) ;; hand—value: Card Card Card —> ?:mbo1 ,Jiu This document is for the exclusive use of j65lu. ...
View
Full Document
 Fall '07
 VASIGA

Click to edit the document details