Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • The basic form for each function that you write, including axillary and local functions, is as follows:
    • Example 5. Function definition for computing area of a ring:
      Code Block
                  ;; area-of-ring : positive-real-number positive-real-number -> positive-real-number
                  ;; Purpose: to compute the area of a ring whose radius is
                  ;; outer and whose hole has a radius of inner
                  ;;
                  ;; Examples:
                  ;; (area-of-ring 5 3) => 50.24
                  ;; (area-of-ring 5 0) => 78.5
                  ;;
                  ;; Template Instantiation: (degenerate)
                  #|
                  (define (area-of-ring outer inner) ...)
                  |#
                  ;; Code:
      
                  (define (area-of-ring outer inner)
                    (- (area-of-disk outer)
                       (area-of-disk inner)))
      
                  ;; Test Examples:
                  (check-expect (area-of-ring 5 3) 50.24)
                  (check-expect (area-of-ring 5 0) 78.5)
                  ...
                  ;; Provide enough examples and tests to show you tested thoroughly
      
    • Example 6. Function definition for computing product of list-of-number:
      Code Block
                  ;; product-of-lon : list-of-numbers -> number
                  ;; to compute the product of numbers in a list
                  ;; assuming product of empty list is 1
      
                  ;; Examples:
                  ;; (product-of-lon empty) => 1
                  ;; (product-of-lon (cons 2 empty)) => 2
                  ;; (product-of-lon (cons 3 (cons 2 empty))) => 6
      
                  ;; Template instantiation
                  #|
                   (define (product-of-lon a-lon)
                     (cond
                       [(empty? a-lon) ...]
                       [(cons? a-lon) ... (first a-lon) ...
                                      ... (product-of-lon (rest a-lon)) ... ]))
                  |#
      
                  ;; Code
                  (define (product-of-lon a-lon)
                    (cond
                       [(empty? a-lon) 1]
                       [(cons? a-lon) (* (first a-lon)
                                         (product-of-lon (rest a-lon)))]))
      
                  ;; Test Examples:
                  (check-expect (product-of-lon empty) 1)
                  (check-expect (product-of-lon (cons 2 empty)) 2)
                  (check-expect (product-of-lon (cons 3 (cons 2 empty))) 6)
                  ;; Provide enough examples and tests to show you tested thoroughly
      
  • Remember to follow the design recipe.
  • It is important that things are presented in this order, so that is clear that you know the correct order for doing things.
  • You are allowed to use the equal? test only for testing. You are not allowed to use it anywhere else in the code.
  • If your examples get too big, then simply define a name for that big argument somewhere before you use it. You can use this name both in your comments in the example section and in the test cases in the Tests section.
  • Be sure to test throughly. Corner cases and edge cases should be tested. For example, when dealing with numerical functions, 0 and 1 are often good test cases.
  • When testing lists, make sure you test the following cases:
    • empty list: empty
    • list with one element: ex: (cons 3 empty)
    • list with more than one element: ex: (cons 1 (cons 3 (cons 3 (cons 7 empty))))
  • Local functions cannot be tested individually, so specific tests are not required for them. However, you main function's tests need to be comprehensive enough to test the local functions.

Sample Solution to a Programming Problem
Anchor
SampleSolution
SampleSolution

The following text is a good solution to the problem of sorting a list of numbers into ascending order; it pulls together all of the specific pieces of design documentation, code documentation, and testing mentioned above. It would be better if it included a few more appropriately chosen tests.

...