WXME0104 ## wxtextwxtabwxmediawximage$(lib "comment-snip.ss" "framework")+(lib "collapsed-snipclass.ss" "framework")drscheme:sexp-snipdrscheme:number,(lib "number-snip.ss" "drscheme" "private")"drscheme:vertical-separator-snip%wxbaddrscheme:xml-snip(lib "xml-snipclass.ss" "xml")drscheme:scheme-snip"(lib "scheme-snipclass.ss" "xml")wxloc%K ZZZZ€ÿ€ÿ€ÿ StandardK Courier New ZZZZ€ÿ€ÿ€ÿF?ð\ZZZ?ð?ð?ð"€‹"Matching Parenthesis StyleF?ð\ZZZ?ð?ð?ð"€‹"F?ð\ZZZ?ð?ð?ð(drscheme:check-syntax:keywordF?ð\ZZZ?ð?ð?ð(F?ðZZZZ?ð?ð?ð€ø@'drscheme:check-syntax:unbound-variableF?ðZZZZ?ð?ð?ð€ø@F?ðZZZZ?ð?ð?ð$$€Œ%drscheme:check-syntax:bound-variableF?ðZZZZ?ð?ð?ð$$€Œ drscheme:check-syntax:primitiveF?ðZZZZ?ð?ð?ð$$€ŒF?ðZZZZ?ð?ð?ð3€‡'drscheme:check-syntax:constantF?ðZZZZ?ð?ð?ð3€‡'F?ðZZZZ?ð?ð?ð€„<$drscheme:check-syntax:baseF?ðZZZZ?ð?ð?ð€„<$F?ðZZZZ?ð?ð?ð?ð?ð?ðXMLF?ðZZZZ?ð?ð?ð?ð?ð?ðK ZZZZ€ÿ€ÿ€ÿK Courier New ZZZZ€ÿ€ÿ€ÿF?ð\ZZZ?ð?ð?ð"€‹"F?ð\ZZZ?ð?ð?ð(F?ðZZZZ?ð?ð?ð€ø@F?ðZZZZ?ð?ð?ð$$€ŒF?ðZZZZ?ð?ð?ð3€‡'F?ðZZZZ?ð?ð?ð€„<$F?ðZZZZ?ð?ð?ð?ð?ð?ðG?ðZZZZ?ð?ð?ð?ð?ð?ðF?ðZZZZ?ð?ð?ðPP€øG?ðZZZZ?ð?ð?ðPP€øG?ðZZZZ?ð?ð?ðdF?ð\ZZZ?ð?ð?ð€¯G?ðZZZZ?ð?ð?ð?ð?ð?ðF?ðZZZZ?ð?ð?ðPP€øG?ðZZZZ?ð?ð?ðPP€øG?ðZZZZ?ð?ð?ðdF?ð\ZZZ?ð?ð?ð€¯#'(define-struct city (name nhbrs seen?)) ;; ;; A city is a  2;; (make-city [symbol] [list-of-cities] [boolean]) 4;; where seen? is to be used later, in path-finding.  ; Initially no neighbors: +(define nyc (make-city 'nyc empty false)) +(define la (make-city 'la empty false)) +(define slc (make-city 'slc empty false)) *(define hou (make-city 'hou empty false)) +(define nome (make-city 'nome empty false)) +(define reno (make-city 'reno empty false))  /(define cities (list nyc la slc hou nome reno))   -(set-city-nhbrs! nyc (list la slc hou nome)) *(set-city-nhbrs! la (list reno slc nyc)) $(set-city-nhbrs! hou (list nyc la)) "(set-city-nhbrs! slc (list reno)) !(set-city-nhbrs! reno (list slc))    0;; path: city, city --> list-of-city or false ,;; Return false if no path, otherwise return (;; a path which includes both endpoints. ;; (define (path src dest) # (cond [(eq? src dest) (list src)] G [else (local {(define otherways (map (lambda (s) (path s dest)) ? (city-nhbrs src))) I (define viable-otherways (filter list? otherways))} 7 (cond [(empty? viable-otherways) false] E [else (cons src (first viable-otherways))]))]))    °(path slc nyc) =  ;; Uh-oh, runs forever! ;; Hand-evaluation reveals 0;; It's checking slc, reno, slc, reno, slc, ...        1;; path3: city, city --> list-of-city or false ,;; Return false if no path, otherwise return (;; a path which includes both endpoints. ;; (define (path3 src dest) 3 (cond [(eq? src dest) (list src)] ;; trivial soln Z [(city-seen? src) false] ;; trivial soln. Already visited, so don't go here again.  [else   (begin A (set-city-seen?! src true) ;; don't revisit this city E (local {(define otherways (map (lambda (s) (path3 s dest)) < (city-nhbrs src))) [ (define viable-otherways (filter list? otherways))} ;; recursive result K (cond [(empty? viable-otherways) false] ;; check if no paths E [else (cons src (first viable-otherways))])))]))    "path3 test cases:" (path3 slc nyc) ;; -> false (path3 nyc nyc) ;; -> (nyc) =(path3 nyc slc) ;; -> (nyc la slc), printed in "shared" form.   ,;;path4: city city -> list-of-list-of-cities ,;; returns a list of paths from src to dest. ;; returns empty if no paths. ";; The paths include src and dest. (define (path4 src dest)  (cond 7 [(eq? src dest) (list (list src))] ;; trivial soln 7 [(city-seen? src) empty] ;; trivial soln  [else  (begin ? (set-city-seen?! src true) ;; don't re-visit this city @ (map (lambda (a-path) (cons src a-path)) ;; add this city 9 (foldr (lambda (a-city rr) ;; recursive call 5 (append (path4 a-city dest) rr))  empty ) (city-nhbrs src))))]))    "path4 test cases:"  ;; reset-cities: --> void ";; resets the seen? field to false ;; in all the cities (define (reset-cities)  (begin  (set-city-seen?! nyc false)  (set-city-seen?! la false)  (set-city-seen?! hou false)  (set-city-seen?! slc false) # (set-city-seen?! reno false)))  (reset-cities) (path4 slc nyc) ;; --> empty (path4 nyc nyc) ;; --> ((nyc)) 3(path4 nyc slc) ;; --> ((nyc slc) (nyc hou la slc))   ,;;path5: city city -> list-of-list-of-cities ,;; returns a list of paths from src to dest. ;; returns empty if no paths. ";; The paths include src and dest. (define (path5 src dest)  (cond 7 [(eq? src dest) (list (list src))] ;; trivial soln 7 [(city-seen? src) empty] ;; trivial soln  [else  (begin A (set-city-seen?! src true) ;; no re-visiting of this city A (map (lambda (a-path) (cons src a-path)) ;; add this city B (apply append ;; combine results A (map (lambda (a-city) ;; recursive call . (path5 a-city dest)) / (city-nhbrs src)))))]))  "path5 test cases:"  (reset-cities) (path5 slc nyc) ;; -> empty (path5 nyc nyc) ;; -> ((nyc)) .(path5 nyc slc) ;; -> ((nyc la slc) (nyc slc))   ,;;path6: city city -> list-of-list-of-cities <;; returns a list of all non-looping paths from src to dest. ;; returns empty if no paths. ";; The paths include src and dest. (define (path6 src dest)  (cond 7 [(eq? src dest) (list (list src))] ;; trivial soln 7 [(city-seen? src) empty] ;; trivial soln  [else  (local 2 [(define rr ;; get the recursive result  (begin Z (set-city-seen?! src true) ;; don't re-visit this city in the recursive call G (map (lambda (a-path) (cons src a-path)) ;; add this city C (apply append ;; combine results B (map (lambda (a-city) ;; recursive call 5 (path6 a-city dest)) 5 (city-nhbrs src))))))]  (begin : (set-city-seen?! src false) ;; clean up mutation + rr))])) ;; return recursive result  "path6 test cases:"  (reset-cities) (path6 slc nyc) ;; -> empty (path6 nyc nyc) ;; -> ((nyc)) h(path6 nyc slc) ;; --> ((nyc la reno slc) (nyc la slc) (nyc slc) (nyc hou la reno slc) (nyc hou la slc)) J"note that this version can run again without resetting the seen? fields." (path6 nyc slc)