Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
is_palindrome x = x == reverse x
|
||||
36
Task/Palindrome-detection/Haskell/palindrome-detection-2.hs
Normal file
36
Task/Palindrome-detection/Haskell/palindrome-detection-2.hs
Normal 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)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
is_palindrome_r x | length x <= 1 = True
|
||||
| head x == last x = is_palindrome_r . tail. init $ x
|
||||
| otherwise = False
|
||||
Loading…
Add table
Add a link
Reference in a new issue