Functions Design Recipe

posted by: Ms. Martin 3 September 2010 No Comment

1. Write down a signature, a purpose statement, and a function header.

A signature is a comment that tells the readers of your design how many inputs your function consumes, from what collection of data they are drawn, and what kind of output data it produces.

purpose statement is a comment that summarizes the purpose of the function in a single line. If you are ever in doubt about a purpose statement, write down the shortest possible answer to the question “what does the function compute?”

2. Illustrate the signature and the purpose statement with some tests. To construct a test, pick one piece of data from each input class from the signature and determine what you expect back.  Use a check-expect function.

3. Formulate how the function computes its results – develop a Scheme expression that uses Scheme’s primitive operations, other functions, and the variables.

Example

(define (area-of-disk r)
  (* 3.14 (* r r)))

;; Signature: 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

;; Tests:
(check-expect (area-of-ring 5 3) 50.24)

;; Definition:
(define (area-of-ring outer inner)
  (- (area-of-disk outer)
     (area-of-disk inner)))
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>