Related material in How to Design Programs: parts I, II
(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:
analyze the problem & identify the classes of data (with examples)
formulate a purpose statement and a contract
make up “functional” examples
create the template: what information exists to compute the desired result?
program
turn the examples into a test suite
(4) over the week, see iterative refinement at work
![]() |
Translate into a function from numbers to numbers:
|
;; 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)
(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 ...)
(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 likec
, 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))
;; 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 oncl
;; 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 ...)))