;; For review: (define-struct city (name nhbrs seen?)) ;; ;; A city is a ;; (make-city [symbol] [list-of-cities] [boolean]) ;; 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 'houston 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 nyc)) (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)) ;; map-append: (alpha --> list-of-beta), list-of-alpha --> list-of-beta ;; Map f to each element of inputs, ;; where f returns a list each time. ;; Return the <em>appended</em> results. ;; (define (map-append f inputs) (foldr (lambda (frst rr) (append (f frst) rr)) empty inputs)) ; An alternate implementation, if you know "apply": ; (apply append (map f inputs)) ;; contains?: list-of-any, any --> boolean ;; Is target in data? ;; Uses eq? for comparison. ;; (define (contains? target data) (ormap (lambda (datum) (eq? datum target)) data)) ; ; Kinda like: (apply or (map .. data)) ;; one-away-from: list-of-city --> list-of-city ;; (define (one-away-from srcs) ...) ;; keep-looking: list-of-city, city --> list-of-city ;; Repeatedly call keep-looking until dest is in srcs. ;; (define (keep-looking srcs dest) (cond [.. ..] ; Base-case [.. ..] ; inductive case: keep-looking one-away-from srcs. (error 'hello-class "Stay tuned for further examples...") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define seed 0) ;; return a pseudo-random int in [0,100) (define (rand) (begin (set! seed (next-num seed)) seed)) ;; next-num: number --> number ;; Return the next number in an (ahem) random sequence. ;; (define (next-num n) (remainder (+ 17 (* n 41)) 100)) (error 'hello-class "Stay tuned for further examples...") ;;;;;;;;;;;;;;;;;;;;; (define directory empty) (define-struct entry (name number)) ;; add-entry!: symbol number --> list-of-entries ;; SIDE-EFFECT: add a new entry to direcotry. ;; We return (the updated) directory. ;; (Not necessary to return this, since anybody can ;; look up the placeholder "directory" at any time, ;; to get that same info.) ;; ;; However, a useful rule of thumb: when writing a function ;; which modifies an object but doesn't otherwise return ;; an interesting value, have it return the object being modified. ;; This is often convenient in allowing you to pass that ;; value to the next function, w/o having to use "begin". ;; (define (add-entry! name number) (begin (set! directory (cons (make-entry name number) directory)) directory)) ;; lookup: symbol --> entry or false ;; (define (lookup name) (lookup-helper name directory)) ;; lookup-helper: symbol, list-of-entries --> entry or false ;; (define (lookup-helper name entries) (cond [(empty? entries) false] [(cons? entries) (if (symbol=? (entry-name (first entries)) name) (first entries) (lookup-helper name (rest entries)))])) (error 'hello-class "Stay tuned for further examples...") ;;;;;;;;;;;;;;;;;;;;;;;;; ;; make-shopper-card: name --> (command, any --> any) ;; (define make-shopper-card (local [(define most-recently-bought #f)] (lambda (name) (lambda (command some-item) (cond [(eq? command 'show-name) name] [(eq? command 'buy) (set! most-recently-bought some-item)] [(eq? command 'show-fave) most-recently-bought])))))