;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname gps1) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp") (lib "convert.ss" "teachpack" "htdp") (lib "guess-gui.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "geocode.ss" "installed-teachpacks"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "world.ss" "teachpack" "htdp") (lib "convert.ss" "teachpack" "htdp") (lib "guess-gui.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "geocode.ss" "installed-teachpacks"))))) ;; Assignment: 1 ;; Name: Kyle Maguire ;; ID: 3564 ;; Name: Gail Terman ;; ID: 4133 |# 1 #| ;; A us-city is a (make-us-city Number Symbol Symbol Number Number) (define-struct us-city (zip name state lat long)) ;; Test and Example cities (define testcity1 (make-us-city 00000 'Teston 'Testachusetts 125 20)) (define testcity2 (make-us-city 11111 'Testville 'Testesee 65 50)) (define testcity3 (make-us-city 99999 'Testix 'Testazona 95 35)) (define stormwind (make-us-city 22222 'Stormwind 'Elwynn 80 20)) (define northshire (make-us-city 33333 'Northshire 'Elwynn 80 40)) (define phoenix (make-us-city 85009 'Phoenix 'Arizona 112.128368 33.456373)) (define boston (make-us-city 02115 'Boston 'Massachusetts 71.092215 42.342706)) |# 2 #| ;; to-posn: us-city -> Posn ;; Consumes a us-city and produces a posn of the location the city ;; would be on a 100 x 100 pixel map of the US (define (to-posn city) (make-posn (* (- (us-city-lat city) 125) (- (/ 5 3))) (* (- (us-city-long city) 50) (- (/ 10 3 ))))) (check-expect (to-posn testcity1) (make-posn 0 100)) (check-expect (to-posn testcity2) (make-posn 100 0)) (check-expect (to-posn testcity3) (make-posn 50 50)) (check-expect (to-posn stormwind) (make-posn 75 100)) |# 3 #| ;; distance: us-city us-city -> Number ;; Consumes 2 us-cities and computes the distance between them (define (distance city1 city2) (sqrt (+ (sqr (* (- (us-city-lat city1) (us-city-lat city2)) 70)) (sqr (* (- (us-city-long city1) (us-city-long city2)) 55))))) (check-expect (distance testcity1 stormwind) 3150) (check-expect (distance testcity2 testcity2) 0) (check-expect (distance stormwind northshire) 1100) (check-within (distance boston phoenix) 2913 2914) |# 4 #| ;; Some trip examples (define trip1 (list testcity1 testcity2 testcity3)) (define trip2 (list phoenix boston stormwind northshire)) (define trip3 (list testcity3 testcity1 testcity2)) (define trip4 (list boston)) ;; total-distance: [Listof us-city] -> Number ;; Consumes a list of us-cities and prodcues the distance ;; traveled visiting each city on the list in order (define (total-distance loc) (local [(define (accum total loc) (cond [(empty? (rest loc)) total] [else (accum (+ (distance (first loc) (first (rest loc))) total) (rest loc))]))] (accum 0 loc))) (check-within (total-distance trip1) 6768 6769) (check-within (total-distance trip2) 5391 5392) (check-within (total-distance trip3) 6768 6769) (check-expect (total-distance trip4) 0) |# 5 #| ;; all-states: [Listof us-city] -> [Listof Symbol] ;; Consumes a list of us-cities and produces a list of all ;; the states you would visit when visiting those cities, ;; in order (define (all-states loc) (local [(define (accum lov loc) (cond [(empty? loc) lov] [(not (member (us-city-state (first loc)) lov)) (accum (append lov (list (us-city-state (first loc)))) (rest loc))] [else (accum lov (rest loc))]))] (accum empty loc))) (check-expect (all-states trip1) '(Testachusetts Testesee Testazona)) (check-expect (all-states trip2) '(Arizona Massachusetts Elwynn)) (check-expect (all-states trip3) '(Testazona Testachusetts Testesee)) (check-expect (all-states trip4) '(Massachusetts))