Skip to content

Instantly share code, notes, and snippets.

@masak
Last active August 29, 2015 14:02
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 masak/8918fee408cf8368e405 to your computer and use it in GitHub Desktop.
Save masak/8918fee408cf8368e405 to your computer and use it in GitHub Desktop.

I noticed that in these two years, no-one had gone ahead an written a Haskell version of the task. So I went ahead and did it. Code below.

I had to learn a bit about Haskell date and calendar handling, looping, monads, and comprehensions before succeeding. But RosettaCode and #haskell were both good sports.

I've optimized for clarity. The isFriday condition doesn't need to be pulled out into its own function, but I felt it helped readability a bit.

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

isFriday :: Day -> Bool
isFriday date = weekDay == 5
    where (_, _, weekDay) = toWeekDate date

friday13ths :: [Day]
friday13ths =
    [ date | year  <- [2012..2017]
           , month <- [1..12]
           , let date = fromGregorian year month 13
           , isFriday date ]

main :: IO ()
main = mapM_ print friday13ths
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment