Resources List

The HtDP/2e design recipe, by kind of data

Parts I (Fixed-Size Data) and II (Arbitrarily Large Data). The steps stay the same in every row. What changes is the data definition, and the function template changes with it. The last two rows cover the recipes for compositions.

How a data definition becomes a template

If the data definition……then the template getsMarked like this
distinguishes several sub-classes of dataa cond with one clause per sub-class, each condition picking out its sub-classcond clause
describes a structureone selector expression per fieldselector/constructor
refers to itselfone natural recursion per self-referencerecursion
refers to another data definitiona call to a separate function, with its own template, for that datahelper

One mark does not come from the data definition: a wish is a function the program needs that is not yet designed. It goes on the wish list with a signature, a purpose statement and a header.

Kind of data Data definition Step 1 Signature, purpose, header Step 2 Examples / tests Steps 3 and 6 Function template Step 4
Part I · Fixed-size data
Atomic data§3.1

A name and an interpretation for a built-in class. Nothing to take apart.

; A Temperature is a Number.
; interpretation degrees Celsius
; Temperature -> Temperature
; converts Fahrenheit f to Celsius
(define (f2c f) 0)
(check-expect (f2c 32) 0)
(check-expect (f2c 212) 100)

A few representative inputs.

(define (f2c f)
  (... f ...))
Enumeration§4.3

A finite list of values, so the function must tell them apart.

; A TrafficLight is one of:
; – "red"
; – "green"
; – "yellow"
; TrafficLight -> TrafficLight
; yields the state that follows s
(define (tl-next s) "red")
(check-expect (tl-next "red") "green")
(check-expect (tl-next "green") "yellow")
(check-expect (tl-next "yellow") "red")

One example per element.

(define (tl-next s)
  (cond
    [(string=? "red" s) ...]
    [(string=? "green" s) ...]
    [(string=? "yellow" s) ...]))
Interval§4.4

Sub-classes are ranges of numbers, so the conditions are comparisons.

; A Price falls into one of
; three intervals:
; – 0 through 1000
; – 1000 through 10000
; – 10000 and above
; interpretation the price of an item
; Price -> Number
; computes the tax charged for p
(define (sales-tax p) 0)
(check-expect (sales-tax 537) 0)
(check-expect (sales-tax 1000)
              (* 0.05 1000))
(check-expect (sales-tax 12017)
              (* 0.08 12017))

One per interval, plus the boundaries.

(define (sales-tax p)
  (cond
    [(and (<= 0 p) (< p 1000)) ...]
    [(and (<= 1000 p) (< p 10000)) ...]
    [(>= p 10000) ...]))
Itemization§§4.5–4.6

Sub-classes may be different classes of data, so the conditions are predicates.

; A MaybeGrade is one of:
; – #false
; – a Number between 0 and 100
; interpretation #false means
; "not yet graded"
; MaybeGrade -> Boolean
; is g a passing grade (60 or more)?
(define (passed? g) #false)
(check-expect (passed? #false) #false)
(check-expect (passed? 59) #false)
(check-expect (passed? 60) #true)

One per sub-class, plus boundaries inside a sub-class.

(define (passed? g)
  (cond
    [(false? g) ...]
    [(number? g) ...]))
Structure§§5.4–5.8

One value with several fields, so the template lists every selector.

; (define-struct posn [x y]) built in
; A Posn is a structure:
;   (make-posn Number Number)
; interpretation a point x pixels from
; the left and y pixels from the top
; Posn -> Number
; computes the distance of p
; to the origin
(define (distance-to-0 p) 0)
(check-expect
  (distance-to-0 (make-posn 0 5)) 5)
(check-expect
  (distance-to-0 (make-posn 7 0)) 7)
(check-expect
  (distance-to-0 (make-posn 3 4)) 5)

Build instances with the constructor.

(define (distance-to-0 p)
  (... (posn-x p) ...
   ... (posn-y p) ...))
Itemization of structures§6.1

The previous two combined: clauses on the outside, selectors inside each clause.

(define-struct circ [radius])
(define-struct rect [width height])
; A Shape is one of:
; – (make-circ Number)
; – (make-rect Number Number)
; Shape -> Number
; computes the area of s
(define (area s) 0)
(check-expect
  (area (make-rect 3 4)) 12)
(check-within
  (area (make-circ 1)) pi 0.01)

One per kind of structure.

(define (area s)
  (cond
    [(circ? s)
     (... (circ-radius s) ...)]
    [(rect? s)
     (... (rect-width s) ...
      ... (rect-height s) ...)]))
Part II · Arbitrarily large data
List§§8, 9

The definition refers to itself, so the template involves a recursive call.

; A List-of-names is one of:
; – '()
; – (cons String List-of-names)
; List-of-names -> Boolean
; is "Flatt" on alon?
(define (contains-flatt? alon)
  #false)
(check-expect
  (contains-flatt? '()) #false)
(check-expect
  (contains-flatt? (cons "Find" '()))
  #false)
(check-expect
  (contains-flatt?
    (cons "A" (cons "Flatt" '())))
  #true)

The base case first, then longer lists.

(define (contains-flatt? alon)
  (cond
    [(empty? alon) ...]
    [(cons? alon)
     (... (first alon) ...
      ... (contains-flatt?
            (rest alon)) ...)]))
Non-empty list§9.2

The base case is a one-item list, so the first condition looks at the rest.

; An NEList-of-temperatures is one of:
; – (cons CTemperature '())
; – (cons CTemperature
;         NEList-of-temperatures)
; NEList-of-temperatures -> Number
; computes the sum of the
; given temperatures
(define (sum ne-l) 0)
(check-expect
  (sum (cons 3 '())) 3)
(check-expect
  (sum (cons 1 (cons 2 (cons 3 '()))))
  6)
(define (sum ne-l)
  (cond
    [(empty? (rest ne-l))
     (... (first ne-l) ...)]
    [else
     (... (first ne-l) ...
      ... (sum (rest ne-l)) ...)]))
Natural number§9.3

Same shape as a list: add1 plays the constructor and sub1 the selector.

; An N is one of:
; – 0
; – (add1 N)
; interpretation the counting numbers
; N String -> List-of-strings
; creates a list of n copies of s
(define (copier n s) '())
(check-expect
  (copier 0 "hello") '())
(check-expect
  (copier 2 "hello")
  (cons "hello" (cons "hello" '())))
(define (copier n s)
  (cond
    [(zero? n) ...]
    [(positive? n)
     (... (copier (sub1 n) s) ...)]))
List of structures§§10.2–10.3

The list definition refers to a second definition, so there are two templates and the first calls the second. Lists of lists work the same way.

(define-struct work
  [employee rate hours])
; A Work is a structure:
;   (make-work String Number Number)

; A Low (list of works) is one of:
; – '()
; – (cons Work Low)
; Low -> List-of-numbers
; computes the wages for all
; work records on an-low
(define (wage* an-low) '())
; Work -> Number
; computes the wage for w
(define (wage w) 0)
(check-expect (wage* '()) '())
(check-expect
  (wage* (cons (make-work "Robby"
                          11.95 39)
               '()))
  (cons (* 11.95 39) '()))
(check-expect
  (wage (make-work "Robby" 11.95 39))
  (* 11.95 39))

Examples for each function.

(define (wage* an-low)
  (cond
    [(empty? an-low) ...]
    [(cons? an-low)
     (... (wage (first an-low)) ...
      ... (wage* (rest an-low)) ...)]))
(define (wage w)
  (... (work-employee w) ...
   ... (work-rate w) ...
   ... (work-hours w) ...))
Parts I and II · Compositions
World program§3.6

What stays the same becomes constants and what changes becomes the state. big-bang fixes the handlers' signatures, so the wish list is known in advance. Each handler is then designed with the recipe for the state's kind of data.

; what stays the same: constants
(define WIDTH-OF-WORLD 200)
(define Y-CAR 20)
(define CAR
  (rectangle 40 15 "solid" "red"))
(define BACKGROUND
  (empty-scene WIDTH-OF-WORLD 40))
; what changes: the state
; A WorldState is a Number.
; interpretation the number of pixels
; between the left border of the
; scene and the car
; wish list, one entry per handler

; WorldState -> Image
; places the car into BACKGROUND
; according to ws
(define (render ws) BACKGROUND)
; WorldState -> WorldState
; moves the car by 3 pixels
; for every clock tick
(define (tock ws) ws)

Other handlers: keys WorldState String -> WorldState, mouse WorldState Number Number String -> WorldState, stopping WorldState -> Boolean.

(check-expect
  (render 50)
  (place-image CAR 50 Y-CAR
               BACKGROUND))
(check-expect (tock 20) 23)
(check-expect (tock 78) 81)

Examples for each handler: one state in, one state or image out.

; WorldState -> WorldState
; launches the program from
; some initial state
(define (main ws)
  (big-bang ws
    [to-draw render]
    [on-tick tock]))
; each handler: the atomic template
(define (tock ws)
  (... ws ...))

The main function is the same for every world program. Only the clauses vary.

Design by composition§§3.4, 11

The data definition and the template are those of a plain list. The change comes at step 5: if filling a gap needs its own traversal, its own case analysis or domain knowledge, wish for a function. One function per task.

; A List-of-numbers is one of:
; – '()
; – (cons Number List-of-numbers)

Unchanged from the List row.

; List-of-numbers -> List-of-numbers
; produces a sorted version of alon
(define (sort> alon) alon)

Wish-list entry:

; Number List-of-numbers -> List-of-numbers
; inserts n into the sorted list
; of numbers alon
(define (insert n alon) alon)
(check-expect (sort> '()) '())
(check-expect
  (sort> (list 12 20 -5))
  (list 20 12 -5))
(check-expect
  (insert 5 '()) (list 5))
(check-expect
  (insert 12 (list 20 -5))
  (list 20 12 -5))

The examples for sort> show that combining (first alon) with the sorted rest is a task of its own.

(define (sort> alon)
  (cond
    [(empty? alon) ...]
    [else (... (first alon) ...
           ... (sort> (rest alon)) ...)]))

Step 5, the definition completed with the wish:

(define (sort> alon)
  (cond
    [(empty? alon) '()]
    [else
     (insert (first alon) (sort> (rest alon)))]))

Then design insert from step 1. It gets its own list template. Figure 72 has the finished program.

Completing the recipe

Step 5 fills the gaps in the template to get the function definition. It has no column of its own. For recursive templates, the examples say how to combine (first l) with the result of the natural recursion. Step 6 runs the examples from step 3 as tests, so the two share a column.

Helper or wish?

A helper is called for by the data: one definition refers to another, so the template already contains the call. A wish is called for by the task: the template is complete, and the need shows up only when the examples are used to fill it in.

About the examples

Most of the examples here are taken from the book and shortened. The section numbers follow the 2e version of the text we are using in class.