Question: 1.Model the following system in Haskell Let's model a room reservation system in Haskell. For this, we first need to create a type that represents
1.Model the following system in Haskell
Let's model a room reservation system in Haskell. For this, we first need to create a type that represents a day and the desired time range. For simplicity's sake, we'll just accept "full" hours. For example:
7:00 to 12:00 13:00 to 14:00 21:00 to 23:00 The hours must be modeled as integers (Int) with values from 0:00 to 23:00. The days must be modeled as a simple strictly positive integer.
For example, to create a reservation on the 42nd from 13:00 to 15:00 I could write TimeRange 42 13 15.
Special cases:
When the start time is the same as the end time, we will consider this time range as empty. In these cases the day should be disregarded.
When the start time is (numerically) less than the end time, we must assume that the reservation changed the day. For example: TimeRange 37 14 12 will indicate a reservation that starts on the 37th at 14:00 and lasts until the 38th at 12:00. Therefore, the largest possible representable reserve for our data type is 23 hours.
Implement the TimeRange ADT. Its constructor (with the same name as the type) does not need to check that the values passed as a parameter are valid. Implement an Eq instance for the TimeRange ADT that considers two bands to be equal when their 3 components are equal or when both bands are empty.
Implement smart-constructor mkRange :: Int -> Int -> Int -> Maybe TimeRange Your function should receive the day, start time and end time of the reservation. It should check for invalid cases and return a time range only in cases where the values are consistent.
Time ranges, or more generally any range, can be treated as monoids. Assume that the default operation for this monoid is intersection and that any empty band can be considered a neutral element. Write a monoid instance for TimeRange that matches time bands as in the examples:
TimeRange 43 13 15 with TimeRange 43 14 15 results TimeRange 43 14 15 TimeRange 15 8 9 with TimeRange 15 7 18 results TimeRange 15 8 9 TimeRange 13 21 9 with TimeRange 14 0 10 results TimeRange 14 0 9 TimeRange 42 21 9 with TimeRange 42 17 21 results TimeRange 0 0 0 (or any other empty band) Any empty range with TimeRange 12 12 15 results in TimeRange 0 0 0 (or any other empty band)
Implement the function haConflicts :: [TimeRange] -> Bool that given a list of time bands returns true if there are conflicts between them and false otherwise.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
