[PLT logo] TeachScheme! 2000

July 10-14, 2000

Details for Tuesday morning



  Finished yesterday working with posns, compound data.  Compound data
  arises in many contexts:
  -- phone books
  -- library catalogs
  -- store inventory
  -- periodic table
  How can we make our own forms of compound data (structures)?

  Example: zoo animals
  -- (define-struct boa (name weight food))
     (make-boa 'Slinky 30 'pets)
     (make-boa 'Slim 50 'bushes)

     ; portable? : boa num -> boolean
     ; determine whether boa no heavier than given weight limit

     Re-motivate need for selectors

  What does define-struct provide? [in contracts, use different colors 
	for checked and unchecked types]
  -- make-boa : symbol num symbol -> boa
  -- boa-name : boa -> symbol
  -- boa-weight : boa -> num
  -- boa-food : boa -> food

  make-boa is the [constrictor] constructor
  the others are selectors
  show box diagram; emphasize the label ("boa", not "posn")

  -- (define-struct armadillo (weight dead?))
     ["In Texas, there a'int nothin' in the middle of the road 'cept a
       yellow line and a dead armadilla"]

     ; portable? : armadillo num -> boolean

     ; run-over-by-car : armadillo -> armadillo

  >> DATA DEFINITIONS FOR MIXED DATA <<

  -- An animal is either
       - a boa, or
       - an armadillo

  ; portable? : animal num -> boolean

   --> define-struct also introduces predicates
   CONDITIONAL, each line in the conditional: selectors

  -- Add (define-struct tiger (name sells weight))
     (make-tiger 'Tony 'cereal 150)

   show how this affects template and `portable?'

  >> DESIGN RECIPE (p88) <<
  specify, make examples, define, test

  Second example:
    A geometric shape is either
     - square  (define-struct square (posn side))
     - circle  (define-struct square (posn r))

   ;; area : shape -> number 

  ** Add new variant: 
    - rectangle ++ add a clause 

  ** Change variant's part: 
    - circle r => radius ++ change the clause for circle 

[Point] LAB

Generated on: Thu Aug 31 09:41:36 CDT 2000