CS 2135 (A01) Homework 1: Warming up in Scheme

Due: September 6 (Thursday) at 11:59pm via turnin (assignment name hwk1).

Assignment Goals


The Assignment

Be sure to refer to the Laws of Homework when preparing your solutions.

Writing Scheme Programs with Cond and Structs

A car-rental company bases its rates on three factors: the kind of car rented, the age of the renter, and a renter's membership in certain clubs that have arranged for discounts for their members. The company rents several types of cars; each type is specified according to the style (compact, mid-size, convertible, or mini-van), brand (Buick, GM, Ford), number of doors, and whether or not the car has a CD player.

  1. Develop a data model for rental-car types. Include the define-struct needed to model your car types and three examples of car types created with your model.

  2. State all of the operators (with their contracts) that are created from your define-struct in the previous question.

  3. Develop a data model for rental-car reservations. A reservation contains the renter's last name, date the rental starts, date the rental ends, and the car type being reserved. Include the define-struct needed to model your reservations and three examples of reservations created with your model. You may choose/design your own data model for dates (but be sure to document it!).

  4. Write a program base-rate which consumes a car type (according to your model) and returns the daily rental rate for a car of that type. The rates are according to the following table:

    compact$21.99
    mid-size$27.99
    convertible$36.99
    mini-van$41.99

  5. Write a program age-surcharge which consumes a renters age (at least 18) and returns a number representing the daily surcharge for renters of that age. The surcharges are according to the following table: [note: table edited 9/4 to fix second age range]

    18-21$15
    22-25$5
    anything else 0

  6. Write a program discount which consumes the name of a discount program (a symbol) and returns a number representing the percentage saved on a rental under the indicated program. The returned percentages should follow the table below:

    AAA or Allstate5%
    HOG (Harley Owners Group) 7%
    none (or other) 0%

  7. Write a program rental-rate which takes a car-type, the renter's age, and a rental discount code. This program returns the daily car rental rate adjusted with the surcharges and discounts. The discount applies to the base rate only, not to the age surcharges.

  8. Write a program change-res-car-type, which consumes a reservation and a car-type and returns a reservation with the same name, start, and end dates as the original reservation, but with the new car-type.

Evaluating Scheme Programs

Evaluate each of the following expressions by hand. Show every step. In each expression, indicate the subexpression that is evaluated to obtain the next expression. For example:
        (sqrt (+ (* 3 3) (* 4 4)))
                 ^^^^^^^
      = (sqrt (+ 9 (* 4 4)))
                   ^^^^^^^
      = (sqrt (+ 9 16))
              ^^^^^^^^
      = (sqrt 25)
        ^^^^^^^^^
      = 5

  1. (/ (+ (square a) (square b)) 2) where square is defined as (define (square n) (* n n))

  2. (or (< 4 3) (and (= 5 (+ 2 3)) (> 8 0)))

  3. (and (+ 6 2) false)

  4. (age-surcharge 24) [use your age-surcharge program from above]

  5. (cond [(/ 9 4) 'divided]
          [(+ 9 4) 'added]
          [else 'neither])
    

Debugging Scheme Programs

For each of the following DrScheme error messages (from Beginner language level), describe what code that produces this error message would look like and provide a small illustrative example of code that would yield this error. Your description should not simply restate the error message!
  1. cond: clause is not in question-answer format

  2. reference to undefined identifier: x

  3. illegal application: first term in application must be a function name


What to Turn In

Turn in a single file hwk1.ss containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.


Back to the Assignments page