Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1 @@
is_palindrome x = x == reverse x

View file

@ -0,0 +1,36 @@
import Data.Bifunctor (second)
import Data.Char (toLower)
------------------- PALINDROME DETECTION -----------------
isPalindrome :: Eq a => [a] -> Bool
isPalindrome = (==) <*> reverse
-- Or, comparing just the leftward characters with
-- with a reflection of just the rightward characters.
isPal :: String -> Bool
isPal s =
let (q, r) = quotRem (length s) 2
in uncurry (==) $
second (reverse . drop r) $ splitAt q s
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
(showResult <$> [isPalindrome, isPal])
<*> fmap
prepared
[ "",
"a",
"ab",
"aba",
"abba",
"In girum imus nocte et consumimur igni"
]
prepared :: String -> String
prepared cs = [toLower c | c <- cs, ' ' /= c]
showResult f s = (show s) <> " -> " <> show (f s)

View file

@ -0,0 +1,3 @@
is_palindrome_r x | length x <= 1 = True
| head x == last x = is_palindrome_r . tail. init $ x
| otherwise = False