Palindrome dates: Difference between revisions

Content added Content deleted
Line 1,145: Line 1,145:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Time.Calendar (Day, fromGregorianValid)
<syntaxhighlight lang="haskell">import Data.List (unfoldr)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
import Data.Maybe (mapMaybe)
import Data.Maybe (mapMaybe)
import Data.Time.Calendar (Day, fromGregorianValid)
import Data.Tuple (swap)

-------------------- PALINDROMIC DATES -------------------


palinDates :: [Day]
palinDates :: [Day]
Line 1,158: Line 1,159:
palinDay y = fromGregorianValid y m d
palinDay y = fromGregorianValid y m d
where
where
[m, d] = unDigits <$> chunksOf 2 (reversedDecimalDigits (fromInteger y))
[m, d] = unDigits <$> chunksOf 2
(reversedDecimalDigits (fromInteger y))


reversedDecimalDigits :: Int -> [Int]
reversedDecimalDigits =
unfoldr ((flip bool Nothing . Just . swap . flip quotRem 10) <*> (0 ==))

unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0


--------------------------- TEST -------------------------
main :: IO ()
main :: IO ()
main = do
main =
let n = length palinDates
let n = length palinDates
putStrLn $ "Count of palindromic dates [2021..9999]: " ++ show n
in putStrLn ("Count of palindromic dates [2021..9999]: "
<> show n) >>
putStrLn "\nFirst 15:"
putStrLn "\nFirst 15:" >>
mapM_ print $ take 15 palinDates
mapM_ print (take 15 palinDates) >>
putStrLn "\nLast 15:"
putStrLn "\nLast 15:" >>
mapM_ print $ take 15 (drop (n - 15) palinDates)</syntaxhighlight>
mapM_ print (take 15 (drop (n - 15) palinDates))


------------------------- GENERIC ------------------------

reversedDecimalDigits :: Int -> [Int]
reversedDecimalDigits = unfoldr go
where
go n
| 0 < n = Just $ swap (quotRem n 10)
| otherwise = Nothing

unDigits :: [Int] -> Int
unDigits = foldl ((+) . (10 *)) 0</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Count of palindromic dates [2021..9999]: 284
<pre>Count of palindromic dates [2021..9999]: 284