Whew! WHAT'S GOING ON?
We need order in this chaos. We need a systematic way to design programs.
Question: When we are given a problem and a white sheet of paper, what strategy should we follow to design programs?
We are faced with the problem of a football team:
*--------------------------* | | In ---> | MyProgram | ---> Out | | *--------------------------*The picture suggests that we start by studying the data especially the kind of data that goes in. We write down a rigorous description of all possible inputs. (We may have to be generous and include a few extra elements in this collection, but so be it.)
;; data definition for In ;; data definition for Out ;; MyProgram : In -> Out ;; MyProgram computes the foo from the boo in an In-element (define (MyProgram an-In) ...)
(Input) Data | Simple | Intervals | Compound | Mixed | Self-referential |
numbers | small set of symbols, ---(------]---[------)--- |
a spider: 8 legs, N flies a day, K m3 | an animal is: a spider, a giraffe, a monkey |
a list of symbols: - empty - (cons S L) where S: symbol L: list of symbols |
|
Contracts | f : number -> XYZ |
f : number -> XYZ |
f : spider -> XYZ |
f : animal -> XYZ |
f : list-of-symbol -> XYZ |
Input Examples | random sample | all symbols, all boundary points, interior points for all piecs | a spider: 8 legs, 10 flies a day, 5 m3 | pick one animal from each kind! | at least one example from each clause |
The Output | |||||
Templates | cond-expression | selector expressions | cond plus selectors/clause | cond plus selectors/clause plus recursion | |
Definitions | domain knowledge |
remind yourself of what the program computes (purpose)
annotate each expression with what kind of data it computes for recursive data,
what primitive operations can produce the desired output by combining what we have? use examples, how did you do it there |
|||
Tests | use domain knowledge |
goal: to find errors use examples to make sure that they are covered |
An animal is either - a spider - a giraffe - a monkey A spider is (make-spider 8 5). A giraffe is (make-spider 8 500). A monkey is (make-spider 8 50).
;; fits? : animal number -> boolean (define (fits? an-animal a-number) (cond ((spider? an-animal) ...) ((giraffe? an-animal) ...) ((monkey? an-animal) ...)))
;; how-many-ants? : list-of-symbols -> number (define ...)