CS 1101 - A-term 15

Homework 6 - Hierarchies; Higher-Order Functions

Due: Thursday, October 8 at 5pm

Read the expectations on homework.

Assignment Goals


The Assignment

There are two parts to this assignment. In Part 1, you will define data and functions for a hierarchy. In Part 2, you will use map and filter to re-write some of the list functions we developed a couple of weeks ago.

Problems

Part 1

A river system can be represented as a hierarchy of tributaries. For example, a partial list of the tributaries that feed into the Missouri River include 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, Sheilds, 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 parts per million (ppm), and can range (at sea level) from 0 ppm (the lowest amount of dissolved oxygen) to 12ppm (saturation point).

  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 parts per million, and a list of the tributaries (rivers) that feed into the river.

  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 lower-ph-than that consumes a river system and a number. The function produces a list of the names of each river in the given system that has a pH value lower than the given pH.

  5. Develop a function healthy? that consumes a river system and produces a boolean. The function returns true if every river in the system has a pH between 6.5 and 8.5, and a DO of at least 6ppm.

  6. 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 have been lowered by 0.1.

Part 2

In Homework 4 we developed several functions that were designed to process a list of volunteer organizations. As you solve the following problems, you may use this set of data definitions from Homework 4:
 

(define-struct volunteer-org (type name age consent? license? training hours languages))
;; a VolunteerOrg is a (make-volunteer-org String String Natural Boolean Boolean Natural ListOfString)
;; interp:
;;        VolunteerOrg represents information about a volunteer organization, where
;;        type is the type of organization, such as animal shelter or nursing home
;;        name is the name of the organization
;;        age is the minimum age required for volunteering
;;        consent?  is true if parental consent is required for volunteers under age 18
;;        license? is true if a valid driver's license is required of volunteers
;;        training is the number of hours of training required prior to volunteering
;;        hours is the minimum number of volunteer hours required per week
;;        languages is a list of the languages spoken by clients of the organization

;; an ListOfVolunteerOrg is one of
;; empty, or
;; (cons VolunteerOrg ListOfVolunteerOrg)
Start by copying and pasting the data definitions for VolunteerOrg and ListOfVolunteerOrg to your DrRacket Definitions window.
  1. Using map and/or filter, redefine the function list-license-training that you wrote for Homework 4, that has the following signature and purpose:
    ;; list-license-training:  ListOfVolunteerOrg Natural -> ListOfVolunteerOrg
    ;; consumes a ListOfVolunteerOrg and produces only those items in the list that require a driver's license and
    ;; less than the specified number of hours of training

  2. Using map and/or filter, define a new function names-by-age with the following signature and purpose:
    ;; names-by-age:  ListOfVolunteerOrg Natural -> ListOfString
    ;; consumes a list of volunteer organizations and the age of a volunteer and produces a list of the
    ;; names of all organizations that will accept volunteers of the given age

  3. Using map and/or filter, redefine the function need-spanish-speakers that you wrote for Homework 4, that has the following signature and purpose:
    ;; need-spanish-speakers:  ListOfVolunteerOrg -> ListOfVolunteerOrg
    ;; consumes a list of volunteer organizations and returns a list of those organizations that have clients who speak Spanish
    


What to Turn In

Using web-based turnin, turn in a single file containing all code and documentation for this assignment. 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.