Determining the day of the week

Develop a Scheme procedure day-of-the-week that takes as its argument any date between September 14, 1752 and December 31, 3000, inclusive, and returns the day of the week on which that date falls.

I recommend that you represent a date as a three-element list of integers, with the year number as the first element, the month number as the second element, and the day of the month as the third element. I recommend that you represent each day of the week as a symbol (Monday, Tuesday, etc.) In that case, a typical call to the procedure might look like this:

> (day-of-the-week (list 2001 10 1))
Monday

The Gregorian calendar, which is the one that has been used in America since September 14, 1752, divides each year into twelve months: January (comprising 31 days), February (28 or 29 days), March (31), April (30), May (31), June (30), July (31), August (31), September (30), October (31), November (30), and December (31). The month of February, normally 28 days long, is given a 29th day in leap years. A leap year is one whose number either is evenly divisible by 400 (such as 2000 or 2400) or else is evenly divisible by 4 but not by 100 (such as 1952 or 2348, but not 1800 or 2500).

The week has seven days, which occur cyclically and invariably: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. The span of time that your procedure is expected to cover comprises 455931 days, and begins on a Thursday; since 455931 is a multiple of 7, the span contains a whole number of weeks, so that its last day is a Wednesday, after 65133 repetitions of the weekly cycle.

In addition to the day-of-the-week procedure, you'll need to develop other procedures that perform various simpler parts of the computation. You may design and specify these as you like.

I strongly recommend drawing up a thorough selection of test cases. To assist you in checking your results, I suggest that you use the Linux cal program, which you can run in a terminal-emulation window. If the command line that you use to invoke it includes a month number and a year number, cal prints out a calendar for the specified month:

bourbaki$ cal 11 1978
    November 1978
Su Mo Tu We Th Fr Sa 
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

The calendar that cal supplies for September 1752 looks odd, but is in fact correct; the apparently missing days were dropped in the transition from the Julian to the Gregorian calendar.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/exercises/day-of-the-week.xhtml

Validated as XHTML 1.1 by the World Wide Web Consortium Cascading Style Sheet validated by the World Wide Web Consortium

created October 1, 2001
last revised October 1, 2001

John David Stone (stone@cs.grinnell.edu)