CS 1101: Review on Structures


Study Questions

  1. When should you create a structure (use define-struct)?

  2. Does a structure define data or a form of computation?

  3. What does DrScheme define as a result of a define-struct?

  4. Why do we write data definitions? (Put another way, what would go wrong if we did not write them?)

  5. Is the following code a complete data definition? If not, what is it missing (and why does that matter)?

       (define-struct address (housenum street city state zip))
    
  6. Critique the following code and data definition: do you recommend any changes and if so, why?

       ; An elephant is a (make-elephant weight number)
       (define-struct elephant (number num-tusks))
    
       ;; too-heavy? : elephant -> boolean
       ;; consume elephant and determine if it weighs more than 1000 pounds
       (define (too-heavy? an-ele)
         (> (elephant-number an-ele) 1000))
    
  7. Continuing with the code in the previous problem, what is elephant-num-tusks?

  8. Write test cases for too-heavy? from the code above.

  9. Assume we want to write programs over shapes, where possible shapes are circles (with a radius) and rectangles (with a width and height). Give a data definition and template for shapes.

  10. Write a program area that consumes a shape and produces its area (a number).