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

Due: January 16 (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 cellular phone company defines different service plans around three pieces of information: the region in which the plan is active, the number of minutes included, and whether the plan includes text messaging. The company defines three regions: Massachusetts, New England, and Nationwide.

  1. Develop a data model for service plans. Include the define-struct needed to model plans and three examples of plans 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 customer subscriptions for cellular service. A subscription contains the customer's name, number, service plan, and a discount code (one of none, long-dist-cust, or long-term-cust). Include the define-struct needed to model your subscriptions and three examples of data created with your model. You may choose/design your own data model for phone numbers (but be sure to document it if it's not a built-in kind of data!).

  4. Write a program regional-rate which consumes a region and returns the monthly rate for that region. Regional rates are according to the following table:

    mass$19.95
    new-england$29.95
    usa$39.95

  5. Write a program minutes-surcharge which consumes a number of minutes (assume non-negative) and returns the rate for that number of minutes. The first 300 minutes are free. Each 200 minutes beyond the first 300 costs $12. Your program should return only whole-number multiples of 12. For example, 301 minutes costs $12, 500 minutes costs $12, and 600 minutes costs $24.

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

    long-dist-cust5%
    long-term-cust 3%
    none 0%

  7. Write a program subscription-rate which takes a subscription and returns the total rate for that subscription. The subscription rate applies the discount to the combination of the regional rate and the minutes surcharge. Text messaging costs $3 extra and is not discounted.

  8. Write a program install-discount, which consumes a subscription and a discount code and returns a subscription with the same name, number, and plan as the original subscription, but with the given (new) discount code.

Evaluating Scheme Programs

Evaluate each of the following expressions by hand (use the rules covered in class, which match those of Beginner level). 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
If an expression would result in an error, show all of the steps up to the error, then indicate the error message you'd get (error messages don't need to be verbatim, as long as they convey the right kind of error).

  1. (/ (- (* 16 16) (double a)) 2) where double is defined as (define (double n) (* n 2))

  2. (or (< 7 2) (and (= 15 (- 18 3)) (> 8 4)))

  3. (and (+ 9 -1) false)

  4. (regional-rate 'new-england) [use your regional-rate program from above]

  5. (cond [(- 5 2) 'subtracted]
          [(* 5 2) 'multiplied]
          [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: expected a clause with a question and answer, but found a clause with only one part

  2. reference to undefined identifier: x

  3. function call: expected a defined name or a primitive operation name after an open parenthesis, but found a number


What to Turn In

Turn in a single file hwk1.ss or hwk1.scm 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