Archive for March 30th, 2010

Programming Praxis – Passover

March 30, 2010

In today’s Programming Praxis exercise we have to write functions to calculate the dates of the Jewish holidays Rosh Hashanah and Passover. Let’s get started.

As usual, some imports:

import Data.Time.Calendar
import Data.Time.Calendar.WeekDate
import Data.Fixed

We start with the function to calculate on which day of September (or October) Rosh Hashanah falls. Unfortunately, because in Haskell we can’t just add integers and floats together, there is a bit of inelegance due to the two fromIntegrals. Nothing too serious, though.

roshDay :: Integer -> Integer
roshDay year | elem d [3,5,7] = n + 1
| d == 1 && f >= 23269/25920 && r > 11 = n + 1
| d == 2 && f >= 1367/2160   && r > 6 = n + 2
| otherwise = n
where
y = fromIntegral year
g = mod' y 19 + 1
r = mod' (12 * g) 19
a // b = fromIntegral \$ floor (a / b)
(n, f) = properFraction (y // 100 - y // 400 - 2 +
765433/492480 * r + mod' y 4 / 4 -
(313 * y + 89081) / 98496)
(_,_,d) = toWeekDate . addDays n \$ fromGregorian year 8 31

Once we have that, calculating the actual dates of Passover and Rosh Hashanah is a simple matter of adding the found number of days to the correct starting dates.

roshHashanah :: Integer -> Day
roshHashanah year = addDays (roshDay year) (fromGregorian year 8 31)

passover :: Integer -> Day
passover year = addDays (roshDay year) (fromGregorian year 3 21)

A quick test demonstrates we’re getting the correct dates.

main :: IO ()
main = do print \$ roshHashanah 2010
print \$ passover 2010