Professional Documents
Culture Documents
The University of Western Australia School of Computer Science & Software Engineering
The University of Western Australia School of Computer Science & Software Engineering
The University of Western Australia School of Computer Science & Software Engineering
Lab details
This lab involves calculating with dates: in particular, it involves determining the era-day of a given
date, and conversely the date of a given era-day. The era-day of a date d is the number of days
between 1 January 1 and d. For example, the era-day of 1 January 1 is 1, the era-day of
31 December 1000 is 365242, and the era-day of 12 March 1999 is 729825.
1. A year y is a leap-year iff y is divisible by 400 or if it is divisible by 4 and not divisible by 100.
Define a function isleap : int -> bool that takes a year y and tells us if y is a leap-year.
For example, 1992 and 2000 are both leap-years; 1900 and 1901 are both not leap-years.
2. The era-day of the last day of year y is 365*y plus 1 for each leap-year between 1 and y. We can
use the principle of inclusion and exclusion to count these leap-years. Add all the years that are
multiples of 4; but this includes years like 1900 that aren’t leap-years, so subtract all the years
that are multiples of 100; but this excludes years like 1600 that are leap-years, so add all the
years that are multiples of 400.
Define a function daysToEndYear : int -> int that takes a year y and returns the era-day of
31/12/y. E.g., daysToEndYear 1 = 365 and daysToEndYear 1792 = 654515.
3. The number of days between 1 January y and the end of month m in year y is given by (367*m +
5)/12 c, where c is a correction to allow for the possibility that y may be a leap-year. The
correction is 0 if m is January, 1 if m is after January and y is a leap-year, and 2 if m is after
January and y is not a leap-year.
Define a function daysToEndMonth : int*int -> int that takes a month m and a year y and
returns the era-day of last/m/y. E.g.,
daysToEndMonth (9,1792) = 654423 daysToEndMonth (7,622) = 227027.
4. Define a function eraDay : int*int*int -> int that takes a date and returns its era-day.
E.g., eraDay (1,1,1) = 1, eraDay (19,7,622) = 227015,
eraDay (22,9,1792) = 654415 and eraDay (12,3,1999) = 729825.
An application
8. Define a function daysAfter : int -> int*int*int -> int*int*int that takes a number
of days x and a date d and returns the date x days after d. For example,
daysAfter 366 (12,3,1999) = (12,3,2000).
Note: we haven't fully covered multi-argument functions like this, but for now all you need to
know is that you can define them as you'd expect:
let daysAfter n (d,m,y) = // Put your code here