Lecture 5 : Structure Upon Structure

A plane consists of three pieces of information.  Its brand, how many
miles it has flown since its last servicing, and the name of the
mechanic who last serviced it.

Write a data definition for a plane.

	(define-struct plane (brand miles mechanic))

;; a plane is a structure
;;  (make-plane B mi mech)
;; where B is a brand, mi is a number, and mech is a symbol

What does DrScheme create when you perform the definition?

	make-plane, plane-brand, plane-miles, plane-mechanic

Write examples of plane data.

  (make-plane (make-brand 'DC-10 550 282 15000) 1000 'Joe)
  (make-plane (make-brand 'DC-10 550 282 15000) 500 'Jane)

Writing examples like this can get tedious.  Therefore, we'd like a
way to give the brands names and use those in defining planes.

  (define brand1 (make-brand 'DC-10 550 282 15000))

  (make-plane brand1 1000 'Joe)

Write a template for a program that processes planes.

(define (... a-plane ...)
  ( ... (plane-brand a-plane) ...
        (plane-miles a-plane) ...
        (plane-mechanic a-plane) ... ))

Write a program service, which takes a plane and a mechanic's name and
returns a new plane with the updated servicing information.

;; service : plane symbol -> plane
;; return information for plane after servicing
(define (service a-plane mech)
  (make-plane (plane-brand a-plane) 0 mech))

Flying objects

;; A balloon is a structure
;;  - (make-balloon capacity roosters)
;;     where capacity and roosters are numbers

(define-struct balloon (capacity roosters))

;; A ufo is a structure
;;  - (make-ufo capacity planet)
;;     where capacity is a number and planet is a symbol

(define-struct ufo (capacity planet))

;; A flying object is one of
;;  - a plane,
;;  - a balloon, or
;;  - a ufo

Can we re-use capacity like this?  Yes, you can re-use names across
structs.  Remember, DrScheme puts the name of the struct as a prefix
on each datum name, so there's no ambiguity.  You can't define two
structs with the same name though.

;; has-room? : flying-object num -> boolean
;; determine if flying object has enough room for passengers
(define (has-room? afo pass) 
  (cond [(plane? afo) ... (plane-brand afo) ... (plane-miles afo) ...
                      (plane-mech afo) ...]
        [(balloon? afo) ... (balloon-capacity afo) ...
                       (balloon-roosters afo)]
        [(ufo? afo) ... (ufo-capacity afo) ... (ufo-planet afo) ...]))

(define (has-room? afo pass) 
  (cond [(plane? afo) (brand-room? (plane-brand afo) pass)]
        [(balloon? afo) (> (balloon-capacity afo) pass)]
        [(ufo? afo) (> (ufo-capacity afo) pass)]))

;; brand-room? : brand num -> boolean
;; determine if a brand has enough seats for a number of passengers
(define (brand-room? a-brand pass)
  (> (brand-seats a-brand) pass))

We've now seen several examples of using structs to model real world
information.  Its important to realize that there is a difference
between information (in the real world) and data (what we can give to

	Information	     |		Data
         real plane	     |       plane struct
       a human mechanic      |       mechanic name

Computer scientists use data to capture information.  Our job is to
cross the bridge from information to data.  Structs are an important tool
in crossing that bridge.