(* CSCE 314, Homework 1, Provided Code ) ( You might choose to uncomment these, like the…
- User
(* 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
- User
please write correct implementations of the incorrect functions
- Assistant
- User
- Assistant
Looking at the error messages for questions 8 and 13, we need to fix the type issues that are happening during compilation. We also need to check challenge problem number 14 and ensure leap years are properly handled.
- User
redo 8 and 14 without match statements
- Assistant
Certainly! Here are the corrected versions of Questions 8 and 14 without using match statements:
Question 8 Correction (Without Using Match Statements)
(*val number_before_reaching_sum : int -> int list -> int = <fun>*) let number_before_reaching_sum (sum : int) (lst : int list) : int = let rec aux (acc_sum : int) (numbers : int list) (count : int) : int = if acc_sum >= sum then count else let hd = List.hd numbers in let tl = List.tl numbers in aux (acc_sum + hd) tl (count + 1) in aux 0 lst 0Challenge Question 14 Correction (Without Using Match Statements)
(*val reasonable_date : int * int * int -> bool = <fun>*) let reasonable_date (date : int * int * int) : bool = let is_leap_year year = (year mod 400 = 0) || (year mod 4 = 0 && year mod 100 <> 0) in let days_in_month month year = if month = 2 then (if is_leap_year year then 29 else 28) else if month = 4 || month = 6 || month = 9 || month = 11 then 30 else if month = 1 || month = 3 || month = 5 || month = 7 || month = 8 || month = 10 || month = 12 then 31 else -1 in let day = fst3 date and month = snd3 date and year = thd3 date in if year <= 0 then false else if month < 1 || month > 12 then false else if day < 1 || day > days_in_month month year then false else true - User
(8) (Write a function number_before_reaching_sum that takes an int called sum, which you can assume is positive, and an int list, which you can assume contains all positive numbers, and returns an int. You should return an int n such that the first n elements of the list add to less than sum, but the first n + 1 elements of the list add to sum or more. Assume the entire list sums to more than the passed in value; it is okay for an exception to occur if this is not the case.) (val number_before_reaching_sum : int * int list -> int = )
let number_before_reaching_sum ((sum : int), (lst : int list)) : int = let rec myFN (acc_sum : int) (numbers : int list) (count : int) : int = if acc_sum >= sum then count else let hd = List.hd numbers in let tl = List.tl numbers in myFN (acc_sum + hd) tl (count + 1) in myFN 0 lst 0
8 fails without any explanation
- Assistant
Based on the specifications, it seems that the function
number_before_reaching_sumshould take two parameters separately, not as a tuple. Also, we must account for the case when the list is empty to avoid a runtime exception when usingList.hdandList.tlon an empty list. Here is the corrected implementation:let number_before_reaching_sum (sum : int) (lst : int list) : int = let rec aux acc_sum numbers count = if acc_sum >= sum || numbers = [] then count else aux (acc_sum + List.hd numbers) (List.tl numbers) (count + 1) in aux 0 lst 0By checking if
numbersis an empty list before callingList.hdandList.tl, we avoid the runtime error potentially caused by an empty list. Additionally, notice that the input is now two separate parameters rather than a tuple, as per the function signatureval number_before_reaching_sum : int -> int list -> int. This should fix the issues related to the function definition and invocation.Lastly, keep in mind best practices for coding in OCaml, such as avoiding the use of and on potentially empty lists. It’s generally safer to use pattern matching or other methods to ensure that the list is non-empty. However, since the problem statement seems to accept this behavior, the current solution fits within the constraints provided.
- User
There must be only one input as a tuple, and we can't use pattern matching
- Assistant
Understood. If you need to have only one input as a tuple and you can't use pattern matching, you have to manage conditions to ensure your function does not call
List.hdandList.tlon an empty list. This means you will add checks to terminate the recursion when you reach an empty list.Here is a corrected implementation with a single tuple input and without the use of pattern matching:
let number_before_reaching_sum ((sum : int), (lst : int list)) : int = let rec aux acc_sum numbers count = if acc_sum >= sum || numbers = [] then count else let head = List.hd numbers in let tail = List.tl numbers in aux (acc_sum + head) tail (count + 1) in aux 0 lst 0This function checks if the accumulator
acc_sumhas reached or exceeded the targetsumor whether thenumberslist is empty before attempting to useList.hdandList.tl. If the recursion should not continue, it returns the current count.