SHOW:
|
|
- or go back to the newest paste.
1 | (* | |
2 | Define a date-like triple to be a value of type int*int*int. Examples of date-like triples | |
3 | include (2013, 2, 1) and (0,0,1000). A date is a date-like triple whose first part is a | |
4 | positive year (i.e., a year in the common era), second part is a month between 1 and 12, | |
5 | and third part is a day between 1 and 31 (or 30, 29, or 28, depending on the month and year). | |
6 | (2013, 2, 1) is a date; (0,0,1000) is not. | |
7 | ||
8 | Write a function is_before that takes two dates as input and evaluates to true or false. | |
9 | It evaluates to true if the first argument is a date that comes before the second argument. | |
10 | (If the two dates are the same, the result is false.) | |
11 | ||
12 | Your function needs to work correctly only for dates, not for arbitrary date-like triples. | |
13 | However, you will probably find it easier to write your solution if you think about making | |
14 | it work for arbitrary date-like triples. For example, it's easier to forget about whether | |
15 | the input is truly a date, and simply write a function that claims (for example) that | |
16 | January 100, 2013 comes before February 34, 2013—because any date in January comes before | |
17 | any date in February, but a function that says that January 100, 2013 comes after | |
18 | February 34, 2013 is also valid. You may ignore leap years. | |
19 | *) | |
20 | let is_before a b = | |
21 | - | let amonth, aday, ayear = a in |
21 | + | let ayear, amonth, aday = a in |
22 | - | let bmonth, bday, byear = b in |
22 | + | let byear, bmonth, bday = b in |
23 | ayear < byear || | |
24 | (ayear = byear && amonth < bmonth) || | |
25 | (ayear = byear && amonth = bmonth && aday < bday) | |
26 | ||
27 | ||
28 | (* | |
29 | Write a function earliest : (int*int*int) list -> (int*int*int) option. It evaluates | |
30 | to None if the input list is empty, and to Some d if date d is the earliest date in | |
31 | the list. Hint: use is_before. | |
32 | ||
33 | As in the previous exercise, your function needs to work correctly only for dates, | |
34 | not for arbitrary date-like triples. | |
35 | *) | |
36 | let earliest l = | |
37 | let rec f d l' = | |
38 | match l' with | |
39 | | [] -> d | |
40 | | d' :: ds -> | |
41 | if is_before d d' then | |
42 | f d ds | |
43 | else | |
44 | f d' ds | |
45 | in | |
46 | match l with | |
47 | | [] -> None | |
48 | | d :: ds -> Some (f d ds) |