;; 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))
;; 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)]
[else
(local {(define otherways (map ...
...))
(define viable-otherways (filter list? otherways))}
(cond [(empty? viable-otherways) false]
[else (cons src (first viable-otherways))])))]))
(path3 slc nyc) = false
(path3 nyc slc) = ; nyc -> la -> slc, printed in "shared" form.
(error 'hello-class "Stay tuned for further examples...")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define speed-limit 75)
(define scofflaw-margin 1.1)
(define my-cruising-speed (* speed-limit scofflaw-margin))
my-cruising-speed = 82.5
;(set! speed-limit 55)
;
;speed-limit = 55
;my-cruising-speed = '???
(error 'hello-class "Stay tuned for further examples...")
;;;;;;;;;;;;;;;;;;;;;
(define a (+ 2 3))
(define b a)
(set! a 'volkswagen)
b
(define aa (make-posn 2 3))
(define bb aa)
(set-posn-x! aa 99)
bb
Similarly, if you call
translate-posn! (a function which changes the posn passed in)
and pass in bb,
both aa and bb can notice a change.
But:
(define (silly x)
(begin (set! x 17) ; Not actually legal in Advanced, but if it were...
'done))
a
(silly a)
a
aa
(silly aa)
aa
bb
(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])))))