;; The first three lines of this file were inserted by DrRacket. 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 Tuesday) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require "extras.rkt") (require rackunit) ;; posns-translated-by-posn : ListOfPosn Posn -> ListOfPosn ;; GIVEN: a list of posns and a posn ;; RETURNS: the list of posns you get by adding the second posn to each of the posns in the first list. ;; EXAMPLE: ((x1, y1), (x2, y2), (x3, y3)) (dx, dy) => ;; ((x1+dx, y1+dy), (x2+dx, y2+dy), (x3+dx, y3+dy)) (define (posns-translated-by-posn lop delta) (map (lambda (p) (make-posn (+ (posn-x p) (posn-x delta)) (+ (posn-y p) (posn-y delta)))) lop)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Improvement #1 ;; HOFC (define (posns-translated-by-posn lop delta) (map (lambda (p) (posn-translated-by-posn p delta)) lop)) ;; Posn Posn -> Posn ;; SD on delta : Posn (define (posn-translated-by-posn p delta) (posn-translated-by-xy p (posn-x delta) (posn-y delta))) ;; Posn Number Number -> Posn ;; SD on p : Posn (define (posn-translated-by-xy p dx dy) (make-posn (+ (posn-x p) dx) (+ (posn-y p) dy))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; SD on delta : Posn (define (posns-translated-by-posn lop delta) (posns-translated-by-xy lop (posn-x delta) (posn-y delta))) ;; posns-translated-by-xy : ListOfPosn Number Number -> ListOfPosn (define (posns-translated-by-xy lop dx dy) (map (lambda (p) (posn-translated-by-xy p dx dy)) lop)) ;; HOFC -- nobody's looing inside the ball here. (ormap (lambda (b) (ball-closer-to-corner-than? b delta)) lob) ;; ball has a selected? field, so we ARE looking inside the ball right her ;; SD on b:Ball (ormap (lambda (b) (ball-selected? b)) lob) ;; this is the same. ;; SD on Ball (ormap ball-selected? lob) ; (... (ball-x b) (ball-y b) (ball-selected? b)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Data Definitions ;; Spaceship (define-struct mother-ship (name crew daughters)) (define-struct drone ()) ;; A Ship is one of ;; (make-mother-ship String ListOfMartian ListOfShip) ;; name is the name of the ship ;; crew is the list of crewmembers of the ship ;; daughters is the list of daughter ships ;; OR ;; (make-drone) ;; Template ;; ship-fn : Ship -> ?? (define(ship-fn s) (cond [(mother-ship? s) (... (mother-ship-name s) (lom-fn (mother-ship-crew s)) (los-fn (mother-ship-daughters s)))] [(drone? s) ...])) ;; A ListOfShip is either ;; empty ;; (cons Ship ListOfShip) ;; template: ;; los-fn : ListOfShip -> ?? (define (los-fn los) (cond [(empty? los) ...] [else (... (ship-fn (first los)) (los-fn (rest los)))])) (define-struct martian (name)) ;; A Martian is a (make-martian String) ;; template: ;; martian-fn : Martian -> ?? (define (martian-fn m) (... (martian-name m))) ;; A ListOfMartian is one of ;; empty ;; (cons Martian ListOfMartian) ;; template ...omitted.... ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Problem #1 ;; ship-name-count : Ship String -> NonNegInt ;; GIVEN: a string ;; RETURNS: the number of crewmembers with that name in either this ship or its fleet. ;; STRATEGY: sd on ship (define (ship-name-count s name) (cond [(mother-ship? s) (+ (crew-name-count (mother-ship-crew s) name) (los-name-count (mother-ship-daughters s) name))] [(drone? s) 0])) ;; crew-name-count : ListOfMartian String -> NonNegInt ;; RETURNS: the number of martians in the list that have that name ;; Strategy: HOFC (define (crew-name-count lom name) (foldr ;; Martian NonNegInt -> NonNegInt (lambda (m count) (if (martian-has-name? m name) (+ 1 count) count)) 0 lom)) ;; los-name-count : ListOfShip String -> NonNegInt ;; RETURNS: the number of martians with that name in the crew of a ship in the list, or in any of their fleets. ;; STRATEGY: HOFC (define (los-name-count los name) (foldr ;; Ship NonNegInt -> NonNegInt (lambda (s count) (+ (ship-count-name s name) count)) 0 los)) ;; martian-has-name? : Martian String -> Boolean ;; ship-has-crew-named? : Ship String -> Boolean ;; SD on ship (define (ship-has-crew-named? s name) (cond [(mother-ship? s) (ormap (lambda (m) (martian-has-name? m name)) (mother-ship-crew s))] [(drone? s) false])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ship-filter-fleet : Ship Name -> Ship ;; SD on Ship (define (ship-filter-fleet s name) (cond [(mother-ship? s) (make-mother-ship (mother-ship-name s) (mother-ship-crew s) (los-filter-fleet (mother-ship-daughters s)))] [(drone? s) s])) ;; foldr: (X Y -> Y) Y ListOf -> Y ;; los-filter-fleet : ListOfShip Name -> ListOfShip ;; RETURNS: a list like the original, except that any ship with the given name in ;; its crew is replaced by its daughters (similarly filtered). ;; Strategy: HOFC (define (los-filter-fleet los name) (foldr ;; Ship ListOfShip -> ListOfShip (lambda (s ans-for-rest) (if (ship-has-crew-named? s name) ;; if we get here, s must be a mother ship (append (mother-ship-daughters s) ans-for-rest) (cons s ans-for-rest))) empty (map (lambda (s) (ship-filter-fleet s name)) los)))