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

Due: January 22 (Thursday) at 11:59pm via turnin (assignment name hwk1).

Assignment Goals

Recall the Laws of Homework and the academic honesty policy when preparing your solutions.

The Assignment

Writing Scheme Programs with Cond and Structs

Your intro engineering professor has asked you to design a small bridge. You can use five components: 2-hole wood slats, 3-hold wood slats, pairs of bolts and nuts, washers, and steel mesh. Your grade on the assignment will be based on a combination of the weight and number of components used to build your bridge, as well as the maximum load (weight) that your bridge can support.

As you plan to consider several designs, you decide to write some programs to compare your designs to maximize your score (this is adapted from an actual intro engineering course assignment at another school).

  1. Develop a data model for bridges. Your model only needs to capture the number, not the configuration, of the various components. Include the define-struct needed to model bridges and three examples of plans created with your model.

  2. Develop a data model for a design. A design should contain a bridge and the maximum weight that it held.

  3. State all of the operators (with their contracts) that are created from your define-struct for designs.

  4. Write a function part-weight that consumes the name of a part (bridge component) and returns the weight of one unit of that component. The weights for single units are as follows:

    ComponentWeight (grams)
    Wood slat, 3 hole2.78
    Wood slat, 2 hole2.82
    Bolt/nut pair:5.15
    Steel mesh, per square inch:0.51

  5. Write a function total-components that consumes a bridge and returns the total number of component units in the bridge.

  6. Write a function total-weight that consumes a bridge and returns the total weight of the bridge.

  7. Write a function design-score that consumes a design and returns the overall score for that design. Compute the score for a design with the following formula:

    Maximum weight held [grams]
    SCORE =
    Weight of bridge [grams] + (2 * Number of pieces)

  8. A truss is a common structure in a bridge design. A simple truss is made of a triangle of three 2-hole wood slats, fastened together with a nut/bolt pair at each of the three corners. Write a function add-simple-trusses that takes a bridge and a number of trusses to add and returns a bridge. The returned bridge should have the same components as the original bridge plus the given number of new trusses.

  9. Write a program load-to-improve-score that consumes a design and a bridge and determines how much weight the bridge would need to bear in order to yield the same or a higher score than the design. When you test your program, make sure that the returned weight does indeed yield a higher score for the new bridge than the original design. [Note: it's fine to return the load required to match the score on the existing design.]

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).

Include the answers to this question in the same file as your programs from the previous part. You can enclose the entire section in a block comment (to prevent Scheme from processing it) as follows:

    [stuff to comment out, as many lines as you want]


  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. (part-weight 'washer) [use your part-weight 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. Turn in your answers to all three parts (writing programs, evaluating programs, and debugging programs).

Back to the Assignments page