Lecture 5: Why Programming is Like American Football



  1. We emersed you in Scheme programming. You have seen a whole lotta kinds of data: numbers, symbols, truth values, fixed-length lists, lists. And you have seen define, cond, placeholders for arguments (inputs), etc. [Draw picture!]

    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:

    And yes, painting and composing and novel-writing are just like that. We need basic techniques -- and then we can use our creativity to construct neat things using those techniques. (Every other notion of creativity is crap.)

  2. Where do we begin? Because programs are "little machines" that consume and produce data:
    		*--------------------------*
    		|                          |
       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.)

  3. Now we can write down:
    ;; 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) ...)
    


  4. And now? Once we have nailed down the data collections, we make up program examples i.e. examples of what kind of output should be produced for some given input. At a minimum we make up a variety of input examples.
    (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,
    • do simple cases first
    • for inductive cases, write down what the natural recursion computes

    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


  5. Let's do an example: animals (does it fit) and list of symbols (is ant in there?)
    An animal is either 
      - a spider
      - a giraffe
      - a monkey
    
    A spider is (make-spider 8 5).
    
    A giraffe is (make-giraffe 8 500).
    
    A monkey is (make-monkey 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 ...)
    




Matthias Felleisen This page was generated on Fri Apr 9 09:17:38 CDT 1999.