Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Palindrome-dates/Haskell/palindrome-dates.hs
Normal file
41
Task/Palindrome-dates/Haskell/palindrome-dates.hs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import Data.List (unfoldr)
|
||||
import Data.List.Split (chunksOf)
|
||||
import Data.Maybe (mapMaybe)
|
||||
import Data.Time.Calendar (Day, fromGregorianValid)
|
||||
import Data.Tuple (swap)
|
||||
|
||||
-------------------- PALINDROMIC DATES -------------------
|
||||
|
||||
palinDates :: [Day]
|
||||
palinDates = mapMaybe palinDay [2021 .. 9999]
|
||||
|
||||
palinDay :: Integer -> Maybe Day
|
||||
palinDay y = fromGregorianValid y m d
|
||||
where
|
||||
[m, d] = unDigits <$> chunksOf 2
|
||||
(reversedDecimalDigits (fromInteger y))
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
let n = length palinDates
|
||||
in putStrLn ("Count of palindromic dates [2021..9999]: "
|
||||
<> show n) >>
|
||||
putStrLn "\nFirst 15:" >>
|
||||
mapM_ print (take 15 palinDates) >>
|
||||
putStrLn "\nLast 15:" >>
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue