Programming Praxis – Passover

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
About these ads

Tags: , , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: