CS 2510 Spring 2012: Assignment 1

copyright 2012 Felleisen, Proulx, et. al.

Practice Problems

For each of the following problems design four different solutions (functions):

Problems:

  1. Problem 31.3.4 in HtDP
  2. Problem 31.3.6 in HtDP
  3. Problem 31.3.7 in HtDP

Pair Programming Assignment

Problem 1

A database of information on localities (i.e., cities) in the USA gives us the zip code, name, state, and latitude and longitude for each locality. Design a data definition to represent a city in that contains this information for a single locality.

Note 1: We may have several different entries for a given city, for example Boston would include the zip code 02115 and 02116, though each has a slightly different latitude and longitude.

Note 2: Define the location of each locality as a separate data definition (long/lat).

Sample data for some zip codes for the capitals of NE states would be:

Problem 2

We want to draw the cities on a map of the United States. Suppose our map is 100 pixels wide and 100 pixels tall. Design a function, named to-posn, that converts a representation of a location into a posn on the map.

Assume that the latitude and longitude lines are parallel to the Scene boundaries (known as parallel projection). Also assume the following:

Assume that we are focused only on the contiguous continental states (i.e., we omit the beautiful states of Alaska and Hawaii... please accept our appologies).

Problem 3

Modify the function to-posn and name it to-posn-par (-- for parametrized --) so that it consumes the width and the height of the Canvas (with the US map stretching over the entire Canvas).

Problem 4

Design a function distance that computes the distance (in miles) between two cities. Estimates tell us that one degree of longitude (at the USA's latitude) is approximately 55 miles, and one degree of latitude is approximately 70 miles. (Feel free to make more accurate estimates. If you do so, explain how you arrived at your numbers.)

Problem 5

Design a function total-distance that computes the total length of a trip where you visit all cities in a list of cities in the order given in the list (assume you are a bird... or a plane... or superman).

Note: First design the function following the Design Recipe, then modify the function to be in accumulator style. (Hand in only the accumulator style function. Make sure your purpose statement includes a line that explains the role of the accumulator.)

Problem 6

Design a function all-states that produces a list of all the states visited on a given trip through a list of cities. Make sure each state appears in the list only once.

Note 1: Design the function in stages: first follow the Design Recipe with the necessary local function definitions, then optimize the function so it traverses the list of cities only once.

Note 2: You will get partial credit if you hand in only the functions that follow the Design Recipe.

Important: Use check-expects to test your functions.

Combine all data definitions and solutions into one DrRacket file and name it HW1 before submitting. Make sure the beginning of your file contains the header that identifies you and your partner as shown on the coourse web page.