Palindrome dates: Difference between revisions

m
Line 1,145:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Time.CalendarList (Day, fromGregorianValidunfoldr)
import Data.List.Split (chunksOf)
import Data.List (unfoldr)
import Data.Tuple (swap)
import Data.Bool (bool)
import Data.Maybe (mapMaybe)
import Data.Time.Calendar (Day, fromGregorianValid)
import Data.ListTuple (unfoldrswap)
 
-------------------- PALINDROMIC DATES -------------------
 
palinDates :: [Day]
Line 1,158 ⟶ 1,159:
palinDay y = fromGregorianValid y m d
where
[m, d] = unDigits <$> chunksOf 2 (reversedDecimalDigits (fromInteger y))
(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 = do
let n = length palinDates
in putStrLn $ ("Count of palindromic dates [2021..9999]: " ++ show n
<> show n) >>
putStrLn "\nFirst 15:" >>
mapM_ print $ (take 15 palinDates) >>
putStrLn "\nLast 15:" >>
mapM_ print $ (take 15 (drop (n - 15) palinDates)</syntaxhighlight>)
 
 
------------------------- 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}}
<pre>Count of palindromic dates [2021..9999]: 284
9,659

edits