CS 1102 (A05) Homework 1: Warming Up in Scheme

Due: August 31 (Thursday) at 11:59pm via turnin (assignment name hwk1).

Assignment Goals


The Assignment

Remember to follow the Expectations on Homework when preparing your solutions, including the academic honesty policy.

Using Helper Functions

The following code produces bar graphs for three pieces of data.

;; bar-graph : number number number -> image
;; consumes three numbers and produces bar graph of results
;;   NOTE: background image sized for inputs up to 20
(define (bar-graph num-a num-b num-c)
  (overlay/xy (overlay/xy (overlay/xy (rectangle 80 80 'solid 'tan)
                                      -25 (- 40 (* 1/2 3 num-a))
                                      (rectangle 15 (* 3 num-a) 'solid 'red))
                          0 (- 40 (* 1/2 3 num-b))
                          (rectangle 15 (* 3 num-b) 'solid 'blue))
              25 (- 40 (* 1/2 3 num-c))
              (rectangle 15 (* 3 num-c) 'solid 'green)))
  1. Copy this code to your homework file, then create a cleaner version of it using helper functions and constants. Turn in only your final version (with all helpers and constants). You do not need to include a copy of the original code. Your final version should have the same behavior as the original code (don't embellish it, just clean it up).

    Be sure to include contracts, purposes and test cases for your helper functions.

  2. Describe in a few sentences how you went about this exercise. Specifically, how did you decide when to create a helper function or constant?

Writing Scheme Programs with Cond and Structs

TravelersComputer.com specializes in insuring mobile computers, guaranteeing to replace a mobile computer if it is lost or stolen. The company stores several pieces of information on their customers: their name, number of years that they are enrolled, computer information, and which replacement policy they have (express-within 24 hours, regular-within 3 days, discount-within 1 week). Computer information consists of the type of computer (notebook, tablet, pocketpc), disk size in GB, and whether it includes a printer.

Your solutions to the following problems should use helper functions in place of repeated code (we will deduct points otherwise). You may also introduce helper functions to improve the readability of your code.

  1. Develop data models for customers and computers. Include both the define-structs and three examples of data for each struct that you define.

  2. State all of the operators (with their contracts) that are created for either one of your define-struct in the previous question (indicate which one you are using).

  3. Computer type could be represented either as symbols or as strings. What are the advantages and disadvantages to each decision? Which one seems to make more sense and why?

  4. Write a program basic-insurance-rate which consumes a computer type and returns the insurance charge for that type for one year. Charges are given in the following table:

    notebook$100
    tablet$150
    pocketpc$50

  5. Write a program computer-insurance-rate which consumes computer information and produces the total yearly insurance charge for that computer. Charges start with the basic-insurance-rate, plus $.10 per GB of disk. A printer, if present, is an additional $10.

  6. Write a program insurance-due which consumes a customer structure and produces the total insurance charges due for the customer. The total charge should include the yearly rate times the number of years, with an additional 25% surcharge for express service and a 10% savings for discount service.

  7. Write a program add-printer, which consumes a customer (not a computer) and produces a customer. If the customer's computer does not have a printer, then the returned customer's computer has a printer (and all other information remains the same). Otherwise, all of the information in the produced customer should be the same as in the given customer.

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). You can use the Stepper to check your answers, but do the problem manually first to make sure you understand how Scheme works.

  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. (basic-insurance-rate 'tablet) [use your own basic-insurance-rate program and replace 'tablet with "tablet" if your program expects a string]

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 (there is something to turn in for every part). Make sure that both students' names are in a comment at the top of the file.


Back to the Assignments page
This page maintained by Mike Gennert
Department of Computer Science Worcester Polytechnic Institute