CS 1102: Accelerated Intro to Program
Design
Documentation and Data Definition Example
A data definition with examples of the data
;; a coordinate is a structure
;; (make-coord num num)
(define-struct coord (x y))
;; Examples of coords
(make-coord 0 8)
(make-coord -1 4)
A program with a contract, purpose, and test cases
;; move-coord-vertical : coord number -> coord
;; consumes a coordinate and a number by which to move the coordinate in the
;; vertical position and produces a coordinate in the new position.
(define (move-coord-vertical a-coord delta)
(make-coord (coord-x a-coord)
(+ (coord-y a-coord) delta)))
;; Test cases
(move-coord-vertical (make-coord 0 8) 5) ;; expect (make-coord 0 13)
(move-coord-vertical (make-coord 4 -2) 7) ;; expect (make-coord 4 5)
(move-coord-vertical (make-coord -1 4) -4) ;; expect (make-coord -1 0)