Template -- what is it, exactly?

The gist of the template is that it captures all the code you can write, knowing ONLY what the function takes in, but not knowing it's specific purpose. Note that we have a template for every type of data we'll process. (Different functions which process the same type of data will all share the same template.) Examples:
Data Definition Template
Representing data with primitive types:
A name is a symbol
(no template needed for built-in primitive types)
Structs:
(define-struct plane (brand miles mechanic))
;; A plane is a structure
;;   (make-plane brand num sym)
;; where brand is the (previously-defined) structure,
;; miles is how far it's traveled since its last overhaul, and
;; mechanic is the name of the mechanic who made the last overhaul.
"Data extraction":
(define (template-for-plane  a-plane ..)
   ..(plane-brand a-plane)..
   ..(plane-miles a-plane)..
   ..(plane-mechanic a-plane)..  )
(If some of these parts are again themselves structures, do not extract from those! Leave that to a separate function.)
Data definitions made with "or" ("unions"):
;; A kritter is one of
;;  - 'kangaroo
;;  - 'kiwi
;;  - 'kookaburra
;; (and nothing else).
(define (kritter-template beastie)
  (cond [(symbol=? beastie 'kangaroo)   ...]
        [(symbol=? beastie 'kiwi)       ...]
        [(symbol=? beastie 'kookaburra) ...]))
The template is a cond, with one branch per type of data, and a question to distinguish each branch. Note that there are no answers yet, in the template.
Data definitions made with "or", where some/all branches are structures:
;; (previous definitions of plane, peach, ufo
;;  as per lecture.)

;; A flying-object is one of
;;  - a plane,
;;  - a peach, or
;;  - the symbol 'gnat
;;  - a ufo
(define (flying-object-template afo)
  (cond [(plane? afo)  ..(plane-brand afo)..
                       ..(plane-miles afo)..
                       ..(plane-mech afo)..]
        [(peach? afo)  ..(peach-capacity afo)..
                       ..(peach-seagulls afo)..]
        [(symbol? afo) ..]
        [(ufo? afo)    ..(ufo-capacity afo)..
                       ..(ufo-planet afo)..]))
That is, just combine the previous two examples, for this general version.

The Design Recipe

  1. Data Analaysis (how will you represent the problem?)
  2. Examples of data (sample inputs).
  3. Contract, Purpose, Header. (*)
  4. Examples of functions (use the sample inputs) (*)
  5. Template
    1. Case analysis (one cond branch for each case of the input)
      (For cond, first write the questions, then fill in the answers second.)
    2. Data Extraction (For each cond-line: call selecters to pull out any sub-parts of the data.)
      (Think about what type each subpart is. Don't yet actually call any functions on these, though.)
    3. ??? (coming next week!)
  6. Write the body (fill in the template). (*)
    (Ask yourself, "what is the rule for combining sub-parts, to get the desired answer?". You can make it concrete by looking at your examples, and for that specific example attach thought-bubbles to the template, showing what each part means.)
  7. test (*)
(*) The marked steps apply to every function. The unmarked ones apply only to the data definitions. I.e., for the the data definition "flying-object", you'll have one template, but that same template will get re-used for each function, and hence doesn't have to be repeated.