Five weekends: Difference between revisions
Content added Content deleted
(→JS ES6: (edited comment line - type signature)) |
(→{{header|Haskell}}: Added an example using the standard Data.Time libraries) |
||
Line 2,840: | Line 2,840: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
====Without using date libraries==== |
|||
Not using any helper libraries, this code profits from Haskell's lazy evaluation. |
Not using any helper libraries, this code profits from Haskell's lazy evaluation. |
||
Knowing that the first day of 1900 was a Monday, we make an infinit list of days and split it in years and months. |
Knowing that the first day of 1900 was a Monday, we make an infinit list of days and split it in years and months. |
||
Line 2,979: | Line 2,980: | ||
1900, 1906, 1917, 1923, 1928, 1934, 1945, 1951, 1956, 1962, 1973, 1979, 1984, 1990, 2001, 2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074, 2085, 2091, 2096 |
1900, 1906, 1917, 1923, 1928, 1934, 1945, 1951, 1956, 1962, 1973, 1979, 1984, 1990, 2001, 2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074, 2085, 2091, 2096 |
||
</pre> |
</pre> |
||
====Using Data.Time==== |
|||
<lang haskell>import Data.Time (Day, fromGregorian, gregorianMonthLength) |
|||
import Data.Time.Calendar.WeekDate (toWeekDate) |
|||
import Data.List.Split (chunksOf) |
|||
import Data.List (intercalate) |
|||
-- MONTHS WITH FIVE WEEKENDS -------------------------------------------------- |
|||
fiveFridayMonths :: Integer -> [(Integer, Int)] |
|||
fiveFridayMonths y = |
|||
[1 .. 12] >>= |
|||
(\m -> |
|||
[ (y, m) |
|||
| isFriday (fromGregorian y m 1) && gregorianMonthLength y m == 31 ]) |
|||
isFriday :: Day -> Bool |
|||
isFriday d = |
|||
let (_, _, day) = toWeekDate d |
|||
in day == 5 |
|||
-- TEST ----------------------------------------------------------------------- |
|||
main :: IO () |
|||
main = do |
|||
let years = [1900 .. 2100] |
|||
xs = fiveFridayMonths <$> years |
|||
lean = |
|||
concat $ |
|||
zipWith |
|||
(\a b -> |
|||
[ b |
|||
| null a ]) |
|||
xs |
|||
years |
|||
(putStrLn . intercalate "\n\n") |
|||
[ "How many five-weekend months 1900-2100 ?" |
|||
, '\t' : show (length . concat $ xs) |
|||
, "First five ?" |
|||
, '\t' : show (concat (take 5 xs)) |
|||
, "Last five ?" |
|||
, '\t' : show ((reverse . concat) (take 5 (reverse xs))) |
|||
, "How many lean years ? (No five-weekend months)" |
|||
, '\t' : show (length lean) |
|||
, "Which years are lean ?" |
|||
, unlines (('\t' :) <$> ((unwords . (show <$>)) <$> chunksOf 5 lean)) |
|||
]</lang> |
|||
{{Out}} |
|||
<pre>How many five-weekend months 1900-2100 ? |
|||
201 |
|||
First five ? |
|||
[(1901,3),(1902,8),(1903,5),(1904,1),(1904,7)] |
|||
Last five ? |
|||
[(2097,3),(2098,8),(2099,5),(2100,10),(2100,1)] |
|||
How many lean years ? (No five-weekend months) |
|||
29 |
|||
Which years are lean ? |
|||
1900 1906 1917 1923 1928 |
|||
1934 1945 1951 1956 1962 |
|||
1973 1979 1984 1990 2001 |
|||
2007 2012 2018 2029 2035 |
|||
2040 2046 2057 2063 2068 |
|||
2074 2085 2091 2096</pre> |
|||
=={{header|Inform 7}}== |
=={{header|Inform 7}}== |