Start of topic | Skip to actions

How to do your homework

  • Most (70%) of your grade depends on good style and following the recipe (grading is described in detail on the homework grading page). In particular, these points will be based on
    • Following the design recipe carefully and documenting your program as you are taught in course (see below), and
    • Good programming style as taught in the course.

  • All assigned problems should be done in the same .scm file.
  • At the top of every assignment, please put a header with the assignment number, your name and e-mail address, and your partner's name and e-mail address, like this:
;; COMP 210 HW #01
;; Christopher Warrington <chrisw@rice.edu>
;; Gregory Malecha <gmalecha@rice.edu>

Submitting your homework

For submission instructions, please see the SVN Turnin tutorial.

Basic form of a function definition: (Chapter 2 onwards)

  • The basic form for each function that you write, including axillary and local functions, is as follows:
;; area-of-ring : positive-real-number positive-real-number -> positive-real-number
;; 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
;;
(define (area-of-ring outer inner) 
  (- (area-of-disk outer) 
     (area-of-disk inner))) 

"Testing area-of-ring:"  ;; This line helps your grader

(equal? (area-of-ring 5 3) 50.24)
(equal? (area-of-ring 5 0) 78.5)
... ;; 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.

Data Definitions and Templates: (Chapter 6 onwards)

Once data definitions are introduced in class, you will be required to include them in your homework submissions. You need to think (and document) your design for data definitions before you start writing functions that process data of this type. Data definitions should be documented as follows:

;; A shape is either
;; * a triangle (make-triangle b h),
;;   where b and h are positive-reals
;; * a square (make-square s),
;;   where s is a positive-real.
(define-struct triangle (base height))
(define-struct square (side))
;;
;; Examples:
;; (make-triangle 1 2)
;; (make-triangle 2 5)
;; (make-square 4)
;; (make-square 2.5)
;;
;; Template:
#|
;; shape-function : shape -> ...
(define (shape-function ... shape ...)
  (cond [(triangle? shape) ... (triangle-base shape)   ...
                           ... (triangle-height shape) ...]
        [(square? shape)   ... (square-side shape)     ...]))
|#


;; A list-of-numbers is either
;;    empty, or
;;    (cons n lon) 
;; where n is a number, and lon is a list-of-numbers
;;
;; Examples:
;; empty
;; (cons 1 empty)
;; (cons 1 (cons 4 empty))
;;
;; Template:
#|
;; f : list-of-numbers -> ...
(define (f a-lon)
  (cond
    [(empty? a-lon) ...]
    [(cons? a-lon) ... (first a-lon) ...
                   ... (f (rest a-lon)) ... ]))
|#
  • Once your have written your data definition, you can use it in the rest of the assignment.
  • The template for a function that consumes a particular datatype is based only on the type definition, and should not depend in anyway on the particular functions that you define on that type. In other words, there is only one template per data type. This same template is used as the starting point for writing functions that process that data type.
  • If the type is a structure, the template should have the field extraction operations. If the type is a variety, the template needs to have an appropriate cond statement. If the type is recursive, the template tells us how the recursion should be done.

Termination Argument: (Chapter 23 onwards) If the template does not guarantee that a function terminates, you are required to explain why that function will terminate for all possible inputs.

;; quick-sort [real] -> [real]
;; Sorts the given list in ascending order.
;;
;; Examples:
;; (quick-sort empty) => empty
;; (quick-sort '(1)) => '(1)
;; (quick-sort '(1 4 3 5)) => '(1 3 4 5)
;; (quick-sort '(1 4 3 4 2 5)) => '(1 2 3 4 4 5)
;;
;; Termination:
;; At each step, quick-sort partitions the list into three
;; sublists using smaller-than, larger-than, and equal-to.
;; The lists produced by smaller-than and larger-than are
;; sorted using recursive applications. Since the lists
;; produced by smaller-than and larger-than are strictly
;; shorter than the given list, eventually quick-sort 
;; receives and returns the empty list.
(define (quick-sort alon)
  (cond
    [(empty? alon) empty]
    [else (append (quick-sort (smaller-items alon (first alon))) 
                  (equal-to alon (first alon)) 
                  (quick-sort (larger-items alon (first alon))))]))

"Testing quick-sort:"
...

Access Permissions


End of topic
Skip to actions | Back to top
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.