CS 1101 - A-term 16

Homework 5 - Hierarchies

Due: Tuesday, October 4 at 5pm

Read the expectations on homework.

Assignment Goals


The Assignment

A river system can be represented as a hierarchy. For example, a list of some of the tributaries that feed into the Missouri River includes the Jefferson, Sun, Yellowstone, Madison, and Gallatin Rivers. The Jefferson, in turn, is fed by the Beaverhead and Big Hole rivers. The Yellowstone is fed by the Gardner, Shields, and Boulder rivers, and so on. In this set of exercises you will create a data definition for a river and its tributaries, and write programs that answer questions about the quality of the water in the rivers.

Assume that for each river, measurements of the river's pH and DO (dissolved oxygen) levels are available. Such measurements are taken at the confluence of the rivers (the point at which the tributaries converge). pH levels can range from 0 (most acidic) to 14 (most alkaline). The normal range for bodies of water are 6.5 - 8.5. DO is measured in milligrams per liter (mg/L). DO levels are dependent on many factors, including water temperature, salinity, atmospheric pressure, aeration, and bacterial levels. Dissolved oxygen levels can range from less than 1 mg/L to more than 20 mg/L depending on how all of these factors interact.

Copy over the work you did for Lab 5 into your Homework 5 file, and continue with the remaining problems.

  1. Provide data definitions for a river system. For each river in the hierarchy, you should record the following information: the name of the river, the pH of the water, the DO in mg/L, and a list of the tributaries (rivers) that feed into the river. The name of your struct should be river. Make sure you define the fields for a river in the order given in the description.

  2. Provide an example of a river system that starts with a single river and consists of at least two levels in the hierarchy below that. You may use the example given above for the Missouri River, if you wish. (You may make up numbers for pH and DO - for these exercises we're not concerned about the accuracy of the information, just that you can provide a correct model for the information.)

  3. Provide the templates for your data definitions.

  4. Develop a function list-acidic-rivers that consumes a river system and produces a list of string. The function returns a list of the names of rivers in the system that have a pH level lower than 6.5.

  5. Acid rain can lower the pH of water in a river system. Develop a function lower-all-ph that consumes a river system and produces a river system. The river system that is produced is the same as the original, except that the pH of all the rivers in the system has been lowered by 0.1.

  6. Cold-water fish such as trout and salmon are more vulnerable to low DO levels than are warm-water fish. Although they can survive for a short time in water with a DO level of less than 5 mg/L, they will die in water below 3 mg/L. Develop a function cold-water-fish-warning that consumes a river system and produces a string. If all rivers in the system have DO levels at 5mg/L or above, the string produced is "OK". If any of the rivers in the system have a DO level below 3 mg/L, the function produces the string "Deadly". Otherwise, the string produced is "Marginal". You'll need to use helpers here. Think about where you'll want to use the templates.

  7. Write a function find-subsystem that consumes the name of a river and a river system and produces either a river system or false. The function returns the portion of the original river system that has the named river as its root. If there is no river in the system with the given name, the function returns false.

  8. Use find-subsystem to write a function count-acidic-tributaries-of that consumes a river system and the name of a river. The function produces a count of the number of tributaries of the named river which have a pH < 6.5. You may assume the named river exists in the river system. You should count the acidic tributaries only, (don't include the named river itself in your count).


What to Turn In

Submit your .rkt file to InstructAssist. Name your file according to the naming conventions for files. Make sure both partners' names and wpi login names appear in a comment at the top of the file.