Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Last active June 10, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/323aff328d94bbcf1a062cfe7d36c217 to your computer and use it in GitHub Desktop.
Save heathermiller/323aff328d94bbcf1a062cfe7d36c217 to your computer and use it in GitHub Desktop.
Clock printer in Unison

Implement a clock that handles times without dates.

You should be able to add minutes to it.

Two clocks that represent the same time should be equal to each other.

It's a 24 hour clock going from "00:00" to "23:59".

To complete this exercise you need to define the data type Clock and implement the functions:

  • addDelta
  • fromHourMin
  • toString

addDelta adds a duration, expressed in hours and minutes, to a given time, represented by an instance of Clock.

fromHourMin takes an hour and minute, and returns an instance of Clock with those hours and minutes.

toString takes an instance of Clock and returns a string representation of the clock, in 0-padded format like "08:03" or "22:35"

unique type Clock = Clock Nat
fromHourMin : Nat -> Nat -> Clock
fromHourMin h m =
Clock ((h * 60 + m) `mod` 1440)
clockText : Nat -> Text
clockText n =
if n < 10 then "0" ++ toText n else toText n
toString : Clock -> Text
toString c =
m = match c with Clock.Clock n -> n
hours = clockText (m / 60)
minutes = clockText (m `mod` 60)
hours ++ ":" ++ minutes
addDelta : Nat -> Nat -> Clock -> Clock
addDelta hour min c =
m = match c with Clock.Clock n -> n
Clock ((m + (hour * 60) + min) `mod` 1440)
createTest : Nat -> Nat -> Text
createTest h m = toString (fromHourMin h m)
addTest : Nat -> Nat -> Nat -> Text
addTest h m m' =
fromHourMin h m |>
addDelta (m' / 60) (m' `mod` 60) |>
toString
test> clock.tests.ex1 =
check ( createTest 8 0 == "08:00" )
test> clock.tests.ex2 =
check ( createTest 11 9 == "11:09" )
test> clock.tests.ex3 =
check ( createTest 24 0 == "00:00" )
test> clock.tests.ex4 =
check ( createTest 25 0 == "01:00" )
test> clock.tests.ex5 =
check ( createTest 100 0 == "04:00" )
test> clock.tests.ex6 =
check ( createTest 0 160 == "02:40" )
test> clock.tests.ex7 =
check ( createTest 0 1723 == "04:43" )
test> clock.tests.ex8 =
check ( createTest 25 160 == "03:40" )
test> clock.tests.ex9 =
check ( createTest 201 3001 == "11:01" )
test> clock.tests.ex10 =
check ( createTest 72 8640 == "00:00" )
test> clock.tests.ex11 =
check ( addTest 10 0 3 == "10:03" )
test> clock.tests.ex12 =
check ( addTest 6 41 0 == "06:41" )
test> clock.tests.ex13 =
check ( addTest 0 45 40 == "01:25" )
test> clock.tests.ex14 =
check ( addTest 10 0 61 == "11:01" )
test> clock.tests.ex15 =
check ( addTest 0 45 160 == "03:25" )
test> clock.tests.ex16 =
check ( addTest 23 59 2 == "00:01" )
test> clock.tests.ex17 =
check ( addTest 5 32 1500 == "06:32" )
test> clock.tests.ex18 =
check ( addTest 1 1 3500 == "11:21" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment