Question
In problems 1-4, a date is an SML value of type int*int*int, where the first part is the year, the second part is the month,
In problems 1-4, a date is an SML value of type int*int*int, where the first part is the year, the second part is the month, and the third part is the day. A reasonable date has a positive year, a month between 1 and 12, and a day no greater than 31 (or less depending on the month). Your solutions need to work correctly only for reasonable dates, but do not check for reasonable dates. A day of year is a number from 1 to 365 where, for example, 33 represents February 2. Ignore leap years.
1. Write a function is_older that takes two dates and evaluates to true or false. It evaluates to true if the first argument is a date that comes before the second argument. (If the two dates are the same, the result is false.)
Evaluating a correct solution should generate this binding:
val is_older = fn : (int * int * int) * (int * int * int) -> bool
Test your code with this expression:
val test1 = is_older((1,2,3),(2,3,4)) = true
2. Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the list are in the given month.
Write a function number_in_months that takes a list of dates and a list of months (i.e., an int list) and returns the number of dates in the list of dates that are in any of the months in the list of months. Assume the list of months has no number repeated. Hint: Use your answer to the previous problem.
Evaluating a correct solution should generate these bindings:
val number_in_month = fn : (int * int * int) list * int -> int
val number_in_months = fn : (int * int * int) list * int list -> int
Check your code with these expressions:
val test2 = number_in_month([(2012,2,28),(2013,12,1)],2) = 1
val test3 = number_in_months([(2012,2,28),(2013,12,1),(2011,3,31),(2011,4,28)],[2,3,4]) = 3
3. Write a function get_nth that takes a list of strings and an int n and returns the nth element of the list where the head of the list is 1st. Do not worry about the case where the list has too few elements: your function may apply hd or tl to the empty list in this case, which is okay.
Write a function date_to_string that takes a date and returns a string of the form January 20, 2013 (for example). Use the operator ^ for concatenating strings and the library function Int.toString for converting an int to a string. For producing the month part, do not use a bunch of conditionals. Instead, use a list holding 12 strings and your answer to the previous problem. For consistency, put a comma following the day and use capitalized English month names: January, February, March, April, May, June, July, August, September, October, November, December.
Evaluating a correct solution should generate these bindings:
val get_nth = fn : string list * int -> string
val date_to_string = fn : int * int * int -> string
Test your code with these expressions:
val test4 = get_nth(["hi", "there", "how", "are", "you"], 2) = "there"
val test5 = date_to_string((2013, 6, 1)) = "June 1, 2013"
4. Write a function oldest that takes a list of dates and evaluates to an (int*int*int) option. It evaluates to NONE if the list has no dates and SOME d if the date d is the oldest date in the list.
Evaluating a correct homework solution should generate these bindings:
val oldest = fn : (int * int * int) list -> (int * int * int) option
Test your code with these expressions:
val test6 = oldest([(2012,2,28),(2011,3,31),(2011,4,28)]) = SOME (2011,3,31)
val test7 = oldest([]) = NONE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started