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

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

Assignment Goals

• To make sure you understand when to use constants and helper functions.
• To make sure you can write programs using conditionals, structures, and nested structures.

The Assignment

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

A company that organizes and runs conferences stores several pieces of information on the people who have registered for the conference: their name, registration category (regular, student, or member), hotel information, and whether they plan to attend the banquet dinner. Hotel information consists of the number of nights needed, what rating of hotel is requested (a number of stars from 2 to 4, inclusive), and the room type needed (double or king).

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 hotel information and conference registrants. 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 one of your define-struct in the previous question (indicate which one you are using).

3. Registration categories 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 `registration-rate` which consumes a registration category and returns the registration fee for that region. Fees are given in the following table:

 regular \$400 member \$350 student \$150

5. Write a program `hotel-surcharge` which consumes hotel information and produces the total charge for that hotel request (for all nights). One night starts at \$109 for a double and \$89 for a king at a 2-star hotel. Rates go up \$30 per night for each room type for each additional star (so a 4-star king room should cost \$149 a night). Compute the charges based on the formula given, rather than by explicitly coding in the rates for each room-type and star combination.

6. Write a program `fees-due` which consumes a conference registrant structure and produces the total fees due for the registration. The total fees should include the registration fee, hotel surcharge, and \$40 if the person plans to attend the banquet.

7. Write a program `upgrade-hotel`, which consumes a conference registration (not a hotel info) and a number (new number of hotel stars) and returns a conference registration. If the given number is larger than the number of hotel stars currently requested, the produced registration has the new number of hotel stars (and all other information the same). Otherwise, all of the information in the produced registration should be the same as in the given registration.

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. `(registration-rate 'member)` [use your own registration-rate program and replace 'member with "member" if your program expects ]

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