Review the grammar so far; introduce values.
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)?
Recall separation between domain knowledge and computing knowledge.
Consider a non-CS-like example:
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
RECAP format of examples: some forget that they are code
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")
PVD 2001:
To deal with the "(make-...) looks like a process, not an answer"
confusion, one teacher pointed out that it was no different from
claiming that sqrt(3) [in math notation] is a number. Nobody denies
that; yet it too is clearly a "process". We thought this was an
excellent response.
PVD 2001:
On Kyle's suggestion (thanks, Matthew!), from the second data def using
structs onward, we switched from the book style of enumerating all the
fields to just writing
A boa is a structure
(make-boa symbol number symbol)
We liked this a lot, and used it the rest of the week. It also saves
precious time and board space.
-- (define-struct armadillo (weight dead?))
["In Texas, there a'int nothin' in the middle of the road 'cept a
yellow line and a dead armadillah"]
; run-over-by-truck : armadillo -> armadillo
>> DATA DEFINITIONS FOR MIXED DATA <<
-- An animal is either
- a boa, or
- an armadillo
Your friend the zoo-keeper wants a program to check whether animals
are above their recommended minimum weights. Min weight for boa is
25 and min weight for dillo is 5.
; too-thin? animal -> boolean
PVD 2002:
We introduced this instead of portable? on animals.
On portable? on animals, some teachers always want a generic weight
selector, since the answer formula is the same for both animals
sans the weight selector. Nobody even raised the issue with this
example, since the conds clearly needed to do different things.
This also reinforces the domain-knowledge point.
--> define-struct also introduces predicates
CONDITIONAL, each line in the conditional: selectors
>> DESIGN RECIPE (p88) <<
specify, make examples, define, test
PVD 2002: At the end of the week, teachers mentioned that summaries
of the data definition formats and templates would be useful. We
recommend handing those summaries out as we cover each style of data
definition (our samples are linked to the main Wonk page).
|
LAB |