Advertisement
Guest User

Untitled

a guest
May 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Clock (addDelta, fromHourMin, toString) where
  2.  
  3. data Clock = Clock {clockHour :: Int
  4.                     , clockMinute :: Int
  5.                     } deriving (Eq, Show)
  6.  
  7. fromHourMin :: Int -> Int -> Clock
  8. fromHourMin hour min = do
  9.     let
  10.         initialHour =
  11.             if hour == 0
  12.                 then 0
  13.                 else
  14.                     if hour > 0
  15.                         then hour
  16.                         else 24 - (rem (abs hour) 24)
  17.         initialMinutes =
  18.             if min == 0
  19.                 then 0
  20.                 else
  21.                     if min > 0
  22.                         then min
  23.                         else 60 - (abs min)
  24.         -- initialHour and initialMinutes are positive only
  25.         hourMinusMinuteCheck =
  26.             if min < 0
  27.                 then initialHour - 1
  28.                 else initialHour
  29.         secondaryHour =
  30.             if initialMinutes > 0
  31.                 then hourMinusMinuteCheck + (quot initialMinutes 60)
  32.                 else hourMinusMinuteCheck - (quot initialMinutes 60)
  33.         finalHour =
  34.             if secondaryHour > 23
  35.                 then rem secondaryHour 24
  36.                 else secondaryHour
  37.         finalMinutes =
  38.             if initialMinutes == 60
  39.                 then 0
  40.                 else rem initialMinutes 60
  41.     Clock {clockHour = finalHour, clockMinute = finalMinutes}
  42.  
  43. toString :: Clock -> String
  44. toString clock = convertToDoubleDigitString (clockHour clock) ++ ":" ++ convertToDoubleDigitString (clockMinute clock)
  45.  
  46. addDelta :: Int -> Int -> Clock -> Clock
  47. addDelta hour min clock = error "You need to implement this function."
  48.  
  49. convertToDoubleDigitString :: Int -> String
  50. convertToDoubleDigitString int =
  51.     if (int < 10)
  52.         then "0" ++ (show int)
  53.         else (show int)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement