[ANN] Today is Doomsday! (0.1)
Hi everyone,
I wanted to share a small command line program I made to train calculating day of the week:
github.com
GitHub - xsebek/doomsday-hs: Algorithm for computing day of the week mentally.
Algorithm for computing day of the week mentally.
It is based on Conway’s Doomsday algorithm, to mentally calculate year “anchor” (this year it’s Saturday) and then use mnemonics for each month’s “doomsday” (select Saturdays from which to count).
Like today “working 9-to-5”, which also works for September 5th:
It has a REPL made with Haskeline and can show statistics using Granite:
But the fun part was implementing the core logic without depending on external libraries. The result is an eDSL to create an explanation template for either Conway’s or alternative versions:
findYearAnchorNakai =
part "Find the year anchor." $ startingWithYear 'Y' $ \y -> do
c1 <- stepI "first take the century digits" $ 'C' := y `div` 100
c2 <- stepI "and remainder dividing by four" $ 'C' := c1 `mod` 4
d1 <- stepI "then take the last two digits" $ 'D' := y `mod` 100
d2 <- stepI "and remainder dividing by four" $ 'E' := d1 `mod` 4
step "add together to get" $ 'W' := 5 * (c2 + d2 - 1) + 10 * d1
This may look daunting, but after adding each number modulo 7 a weekday pops out.
From technical perspective, I really liked splitting the explanation, evaluation and pretty-printing. Years ago I tried simplifying the math manually in Swift in string interpolation and it was buggy and inconsistent. So I decided to do it right() in Haskell!
I hope some of you find this fun and can use it to amaze your friends and colleagues.
Discussion in the ATmosphere