Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Clock (addDelta, fromHourMin, toString) where
- data Clock = Clock {clockHour :: Int
- , clockMinute :: Int
- } deriving (Eq, Show)
- fromHourMin :: Int -> Int -> Clock
- fromHourMin hour min = do
- let
- initialHour =
- if hour == 0
- then 0
- else
- if hour > 0
- then hour
- else 24 - (rem (abs hour) 24)
- initialMinutes =
- if min == 0
- then 0
- else
- if min > 0
- then min
- else 60 - (abs min)
- -- initialHour and initialMinutes are positive only
- hourMinusMinuteCheck =
- if min < 0
- then initialHour - 1
- else initialHour
- secondaryHour =
- if initialMinutes > 0
- then hourMinusMinuteCheck + (quot initialMinutes 60)
- else hourMinusMinuteCheck - (quot initialMinutes 60)
- finalHour =
- if secondaryHour > 23
- then rem secondaryHour 24
- else secondaryHour
- finalMinutes =
- if initialMinutes == 60
- then 0
- else rem initialMinutes 60
- Clock {clockHour = finalHour, clockMinute = finalMinutes}
- toString :: Clock -> String
- toString clock = convertToDoubleDigitString (clockHour clock) ++ ":" ++ convertToDoubleDigitString (clockMinute clock)
- addDelta :: Int -> Int -> Clock -> Clock
- addDelta hour min clock = error "You need to implement this function."
- convertToDoubleDigitString :: Int -> String
- convertToDoubleDigitString int =
- if (int < 10)
- then "0" ++ (show int)
- else (show int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement