1  Monday Morning: Welcome Back

Related material in How to Design Programs: parts I, II

1.1  Goals

(1) recall basic Scheme data: numbers, images, structures, lists

(2) recall basic Scheme mechanics: defining functions, structures, variables

(3) recall the design recipes for structural data:

  1. analyze the problem & identify the classes of data (with examples)

  2. formulate a purpose statement and a contract

  3. make up “functional” examples

  4. create the template: what information exists to compute the desired result?

  5. program

  6. turn the examples into a test suite

(4) over the week, see iterative refinement at work

1.2  Example: Functions on Numbers

[A UFO gliding down]

Translate into a function from numbers to numbers:

t = 0 1 2 3 4 ...7
X = 203040 50 60 ...?

;; ufo-x : Number  →  Number
;; where is the UFO at t (horizontally)?
;; examples: see table 
(define (ufo-x t) 
  (+ 20 (* 10 t)))

;; Tests:
(= (ufo-x 0) 20)
(= (ufo-x 1) 30)
(= (ufo-x 2) 40)

;; Run program run: 
(ufo-x 2345)

1.3  Example: Images

(define an-image (rectangle 3 4 'blue))

;; area : Image  →  Number
;; compute the number of pixels in an image
;; example: (area an-image) should be 12
(define (area i)
  (* (image-width i) (image-height i)))

;; Tests:
(= (area an-image) 12)

;; Run program run: (demo matthias demo)
(area ... an image of your favorite teach-scheme instructor ...)

1.4  Example: Structures and Functions on Structures

(define-struct color (red green blue)) ;; color occurs in nature
;; Color = (make-color Number Number Number)

;; make-color; color?; color-red, color-green, color-blue 

#| Template: 
;; \scheme{Color -> ???}
(define (process-rgb c)
  ... (color-red c) ...
  ... (color-green c) ...
  ... (color-blue c) ... )
|#

;; Program: 
;; Color  →  Color
;; produce a color like c, but without any green 
;; example: (remove-green (make-color 200 100 0)) should be 
;; (remove-green (make-color 200 0 0))
(define (remove-green c)
  (make-color (color-red c) 0 (color-blue c)))

;; Tests: 
(equal? (remove-green (make-color 200 100 0))
        (make-color 200 0 0))

1.5  Example: Lists and Functions on Lists

;; A ColorList is either
;; --- empty
;; --- (cons Color ColorList)

#| Template: 
;; \scheme{ColorList -> ???}
(define (process-cl cl)
  (cond
    [(empty? cl) ...]
    [else ... (first cl) ... (process-cl (rest cl)) ...]))
|#

;; ColorList  →  ColorList
;; remove green from every color on cl
;; examples: see tests
(define (remove-all-green cl)
  (cond
    [(empty? cl) empty]
    [else (cons (remove-green (first cl)) (remove-all-green (rest cl)))]))

;; Tests: 
(equal? (remove-all-green (cons (make-color 100 100 100) 
                                (cons (make-color 200 100 0)
                                      empty)))
        (cons (make-color 100 0 100) 
              (cons (make-color 200 0 0)
                    empty)))

;; Run program run:
( color-list image 
  (remove-all-green
    ( image color-list
      ... an image of your favorite teach-scheme instructor ...)))