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

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

Assignment Goals


The Assignment

Be sure to refer to the Laws of Homework when preparing your solutions.

Writing Scheme Programs with Cond and Structs

Last week, Mars was closer to the Earth than it had been in 59,619 years. People who wanted to see Mars needed to know what time it would rise in the night sky. Given a set of base data about when Mars would rise on given dates, the rise time for a particular location can be computed by adjusting the base data for longitude and latitude. This assignment has you write Scheme programs to perform this calculation.

  1. Develop a data model for latitude and longitute positions (each is defined by a number of degrees and a direction: north or south for latitude, and east or west for longitude). Include the define-struct needed to model positions and three examples of positions created with your model.

  2. State all of the operators (with their contracts) that are created from your define-struct in the previous question.

  3. Develop a data model for locations (of cities, towns, etc). A location stores latitude, longitude, and the time zone (like EST, CDT, etc). Include the define-struct and three examples of data created with your model.

  4. Write a program north-of-40? that determines whether a position is north of 40 degrees latitude (you may assume the input position is a latitude position).

  5. Write a program degrees-south-of-40n that takes a latitude position that is not north of 40 degrees and returns the number of degrees that the position is south of 40-north. For example, a latitude of 10 degrees north is 30 degrees south of 40-north, while a latitude of 10 degrees south is 50 degrees south of 40-north.

  6. Write a program adjust-for-latitude that takes a location and a time (as a number of minutes) and adjusts the time to correct for latitude (returning the new time). The adjustment adds 3 minutes for each degree that the location is north of 40-north and subtracts 2 minutes for each degree that the location is south of 40-north.

  7. The base rising times are reported using a standardized longitude for each time zone. Greenwich Mean Time (GMT) uses a standard of 0 degrees east, Eastern Standard Time (EST) uses a standard of 75 degrees west, and Central Daylight Time (CDT) uses a standard time of 90 degrees west. Write a program time-zone-meridian that consumes a time-zone and returns the standard longitude position for that time zone. (Use the same representation for time zones as in your position examples.)

  8. Write a program adjust-for-longitude that takes a location and a time (as a number of minutes) and adjusts the time to correct for longitude (returning the new time). The longitude adjustment adds 4 minutes for each degree of longitude that a location is west of the standardized longitude for its time zone. If the location is east of the standard longitude, subtract 4 minutes for each degree of difference.

  9. Write a program mars-time that consumes a base time (as a number of minutes) and a location and adjusts the time for the longitude and latitude of the given location.

    As one of your test cases, reproduce the following example (from the space.com webpage that inspired this assignment -- it gives base rise data, called LMT, for several dates). Also include a test case for Worcester, which is at 71.8-west and 42.27 north.

    EXAMPLE: When will Mars rise for Minneapolis, Minnesota on July 29? The LMT for Mars rise is 10:11 p.m. Minneapolis observes daylight saving time and is located at longitude 93.3: west, latitude 45: north. Correction for longitude: 3.3 x 4 = 13.2 (round off to 13). Minneapolis is west of 90:, so you would add 13 minutes to 10:11 p.m., giving 10:24 p.m. Correction for latitude: Minneapolis is 5: north of latitude 40:, so 5 x 3 =15. Add 15 minutes to 10:24 p.m., giving 10:39 p.m. Central Daylight Time.

    For testing, you may find it handy to write functions that convert from hours and minutes into a number of minutes and vice versa (this is not required for the assignment though).

  10. Go outside on a clear night and look towards the southeast to see Mars (viewing will be good through September). With a decent pair of binoculars (or in some cases good eyesight), you can see Mars' reddish tint. It's neat, and Mars won't be this close again until 2287.

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. (time-zone-meridian 'est) [use your regional-rate 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. Make sure that both students' names are in a comment at the top of the file.


Back to the Assignments page