We saw last time, structures. In particular: (define-struct brand (type speed seats service)). This tells DrScheme that we have a new type of data (in addition to numbers, booleans, symbols).
What are some particular values of this type? Let's create some brands:
; Examples of the data: ; (make-brand 'DC-10 550 282 15000) (make-brand 'MD-80 505 141 10000) (make-brand 'ATR-72 300 46 5000)
When we define compound data, we need to document what kinds of information are in that form of data. We call this documentation a data definition. Here's the data definition for a brand.
(define-struct brand (type speed seats service)) ;; A brand is a structure ;; (make-brand symbol num num num) ;; where type is the model/make, speed is the cruising speed, ;; seats is the seating capacity, and service is the number ;; of miles before an overhaul is required. ;; type is the modeYou must write a data definition every time you use define-struct.
Let's rewrite our max-dist function so that the input is now a brand structure, rather than a brand name. Note that we now need a way to access information inside the structure. When we evaluated the define-struct, DrScheme also created these accessor functions for us.
;; max-dist : brand num -> num ;; compute the maximum distance that a brand can fly in a given number ;; of hours (define (max-dist a-brand hours) (* (brand-speed a-brand) hours)) ;; Examples (max-dist (make-brand 'DC-10 550 282 15000) 2) = 1100Summary: When DrScheme evaluates (define-struct brand (type speed seats service)), it creates five new functions, behind the scenes: make-brand, brand-type , brand-speed, brand-seats, brand-service.
Try another. Write a program needs-service? which consumes a brand and a number of miles and returns true if the brand must be serviced having flown the given number of miles.
;; needs-service? : brand num -> bool ;; determines whether brand needs servicing after flying given miles (define (needs-service? a-brand miles) (>= miles (brand-service a-brand))) ; test cases (needs-service? (make-brand 'DC-10 550 282 15000) 5) = false (needs-service? (make-brand 'DC-10 550 282 15000) 20000) = true (needs-service? (make-brand 'DC-10 550 282 15000) 15000) = true
On a different note, we did a lot of typing there, repeating the construction of the DC-10 structure. Doesn't this go against the spirit of our rule, Don't Repeat Code (a.k.a. re-use existing functions/code)? Besides, what if the FAA changes its rules, requiring DC-10's to be serviced every 13000 miles, instead?
Solution: placeholders (i.e., named constants).
(define kilos-per-pound 2.2) (define pi 3.14) (define dollars-per-euro 0.88) (define cutoff-grade-A 0.88) (define my-favorite-brand (make-brand 'DC-10 550 282 15000)) (define MD-80 (make-brand 'MD-80 505 141 10000))In this last case, what is the difference between MD-80, and 'MD-80? A big one! One is a placeholder, and the other is a symbol. Note: placeholders only help save typing, nothing more. You can always replace a place holder with its defined value -- in fact that's all drscheme does (we'll see the exact rules soon).
In lecture, students have seen a program using cond, and structures, but i have *not* yet used the term "template".
The cond example took in one of three different symbols (a model of airplane), and returned a number (the max-speed of that model). This example was *before* they saw structures.
Then, they have also seen structures -- but we revised our earlier version to make a structure "brand", which comprised a symbol (name) and a number (speed).
The hitch is that if you just say "like the airplane example", you need to be sure to emphasize: