Versions Compared

Key

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

...

  • The basic form for each function that you write is shown below:

    • Example 5. 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 Be careful with regard to using the equal? operation in the program code that you write because the worst case running time is the size of the smaller of its two inputs (on inputs that are equal or nearly equal).  Of course, the equal? operation and operations that call equal? (like check-expect) are extensively used in test 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 throughlyyour code thoroughly. Corner cases and edge cases should be tested. For example, when When dealing with numerical functions, 0 and 1 are often good important test casesinputs.
  • 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.

...