solve 13 and 14 (* CSCE 314, Homework 1, Provided Code ) ( You might choose to uncomment…
- User
solve 13 and 14
(* CSCE 314, Homework 1, Provided Code *)
(* You might choose to uncomment these, like the lecture code does ) ( #utop_prompt_dummy let _ = UTop.set_show_box false *)
(* Use these functions to extract parts of a date ) let fst3 (x,,) = x ( gets the first element of a triple ) let snd3 (,x,) = x ( gets the second element of a triple ) let thd3 (,,x) = x ( gets the third element of a triple *)
(**
- TODO: Complete the 12 function bindings described in the assignment. For the first (2),
- we have given you the correct first line and an incorrect function body. *)
(You will write 12 OCaml functions (and tests for them) related to calendar dates. In all problems, a “date” is an OCaml value of type int * int * int, where the first part is the day, the second part is the month, and the third part is the year. For example, (15, 1, 2021) represents the 15th day of the first month (January) in the year 2021. 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 (that is a challenge problem) and many of your functions will naturally work correctly for some non-reasonable dates. A “day of year” is a number from 1 to 365 where, for example, 33 represents February 2. (We ignore leap years except in one challenge problem.) To access elements of an date, use the provided functions fst3, snd3, and thd3 to access the first, second, and third components accordingly.)
(Solutions should be: Correct In good style, including indentation and line breaks Written using features discussed in class. In particular, you must not use OCaml’s mutable references or arrays. (Why would you?) Also do not use pattern-matching; it is the focus of the next assignment.)
(Here is a “cheatsheet” of library functions and operations that you may find helpful when completing your solution, in addtion to using the provided fst3, snd3, and thd3. List.hd ( get the head of the list ) List.tl ( get the tail of the list ) @ ( infix operator to append two lists ) ^ ( infix operator to concatenate two strings ) string_of_int ( convert an integer into a string ) Option.get ( if the argument = Some value, return value, otherwise raise an exception ) Option.is_some ( return true if the argument = Some value ) Option.is_none ( return true if the argument = None ) 3)
(* 1 ) ( DONE *) (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.)) () let is_older ((date1 : int * int * int), (date2 : int * int * int)) =
- Assistant
To solve challenge problem 13, we'll need to remove duplicates from the list of months before calling . To solve challenge problem 14, we'll need to write a function that checks if a given date is "reasonable" by verifying the year, month, and day according to the rules provided.
- User
don't use match statements. Only use functions given
- Assistant
Given the requirements to avoid using match statements and to use only provided functions and those listed in the cheatsheet, we can rewrite solutions for the challenge problems. Here's the modified version:
For Challenge Problem 13:
ocaml