Palindrome detection: Difference between revisions
m
→{{header|Haskell}}: Slight expansion of tests and output
(→{{header|Python}}: Added a variant which just checks the left half against a reflection of the right half.) |
m (→{{header|Haskell}}: Slight expansion of tests and output) |
||
Line 2,202:
Or, applicative and point-free, with some pre-processing of data (shedding white space and upper case):
<lang haskell>import Data.
import Data.Char (toLower)
------------------- PALINDROME DETECTION -----------------
Line 2,209 ⟶ 2,210:
isPalindrome = (==) <*> reverse
-- with a reflection of just the rightward characters.
▲-- Or, comparing just the first half
isPal s =
▲isPal :: Eq a => [a] -> Bool
in uncurry (==) $
▲ (q, r) = quotRem (length s) 2
▲ (a, b) = splitAt q s
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_ putStrLn $
(showResult <$> [isPalindrome, isPal])
<*> fmap
prepared
[ "",
"a",
"ab",
"aba",
"abba",
]
▲sample = "In girum imus nocte et consumimur igni"
showResult f s = (show s) <> " -> " <> show (f s)</lang>
▲prepared cs = [toLower c | c <- cs, ' ' /= c]</lang>
{{Out}}
<pre>
"a" -> True
"ab" -> False
"aba" -> True
"abba" -> True
"ingirumimusnocteetconsumimurigni" -> True
"" -> True
"a" -> True
"ab" -> False
"aba" -> True
"abba" -> True
"ingirumimusnocteetconsumimurigni" -> True</pre>
|