CS 1101: Programming Language
Concepts
Documentation and Data Definition Example
A data definition with examples of the data
;; a Coordinate is a (make-coord num num)
(define-struct coord (x y))
;; interp: x is the horizontal coordinate
;; y is the vertical coordinate
;; Examples of Coordinates
(define COORD1 (make-coord 0 8))
(define COORD2 (make-coord -1 4))
A program with a signature, purpose, and test cases
;; move-coord-vertical : Coordinate Number -> Coordinate
;; 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
(check-expect (move-coord-vertical (make-coord 0 8) 5) (make-coord 0 13))
(check-expect (move-coord-vertical (make-coord 4 -2) 7) (make-coord 4 5))
(check-expect (move-coord-vertical COORD2 -4) (make-coord -1 0))