For this assignment, we require that you write comments in the style outlined below. It follows "How to design programs", a textbook by the developers of DrScheme/DrRacket. There are 4 required sections: Contract, Purpose, Example, and Definition, as in the example comment for function area-of-ring:

;; Contract: area-of-ring : number * number -> number
;; Purpose: to compute the area of a ring whose radius is
;; outer and whose hole has a radius of inner
;; Example: (area-of-ring 5 3) should produce 50.24
;; Definition:
(define (area-of-ring outer inner)
  (- (area-of-disk outer)
     (area-of-disk inner)))

The goal of designing a function is to create a mechanism that consumes and produces data. Therefore, every function has a meaningful name and a statement of what kind of information it consumes and produces. This is called a CONTRACT and is, essentially, an unchecked type signature of the function. The contract for area-of-ring is:

;; Contract: area-of-ring : number * number -> number

Semicolons indicate that the line is a Scheme comment. The contract consists of two parts. The first, to the left of the colon, states the function's name. The second, to the right of the colon, specifies what kind of data the function consumes and what it produces; the inputs are separated from the output by an arrow.

In addition, every function has a short PURPOSE statement, that briefly describes what the function computes. For most functions, one or two lines will suffice. The purpose statement for our running example area-of-ring is:

;; Purpose: to compute the area of a ring whose radius is
;; outer and whose hole has a radius of inner

The EXAMPLE section shows a call and the expected return result, and the DEFINITION section contains the actual Scheme code for the funtion:

;; Example: (area-of-ring 5 3) should produce 50.24
;; Definition:
(define (area-of-ring outer inner)
  (- (area-of-disk outer)
     (area-of-disk inner)))

Additional Examples

Function map (a higher-order polymorphic function):

;; Contract: map : (a -> b) * (list a) -> (list b)
;; Purpose: to apply function f on each element of list l
;; Example: (map abs '(-1 2 -3)) should produce (1 2 3)
;; Definition:
(define (map f l)
  (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))

The above contract states that map consumes 2 arguments: the first argument is a function that takes a value of type a and returns a value of type b, and the second argument is a list whose elements are of type a. Function map returns a list whose elements are of type b. Here a and b to denote type variables that can be instantiated to arbitrary concrete types. Note however that a and b link the arguments and return type of map: function f takes a type a, and therefore the list l must consists of elements of type a, as f is applied on each element of l; furthermore, the return value must be a list with elements of type b.

Function foldl (another higher-order polymorphic function):

;; Contract: foldl : (b * a -> b) * (list a) * b -> b
;; Purpose: to fold (i.e., reduce) the list l into a single value
;; Example: (foldl + '(1 2 3) 0) should produce 6
;; Definition:
(define (foldl op l id)
  (if (null? l) id (foldl op (cdr l) (op id (car l)))))

Errata

None yet. Check the Announcements page regularly.