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.CharBifunctor (toLowersecond)
import Data.Char (toLower)
 
------------------- PALINDROME DETECTION -----------------
Line 2,209 ⟶ 2,210:
isPalindrome = (==) <*> reverse
 
-- Or, comparing just the firstleftward halfcharacters with
-- with a reflection of just the rightward characters.
 
isPal :: Eq a => [a]String -> Bool
-- Or, comparing just the first half
isPal s =
-- with the reversed latter half:
let (q, r) = quotRem (length s) 2
isPal :: Eq a => [a] -> Bool
in uncurry (==) $
isPal s = a == reverse (drop r b)
second (a,reverse . drop br) =$ splitAt q s
where
(q, r) = quotRem (length s) 2
(a, b) = splitAt q s
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
main = print $ [isPalindrome, isPal] <*> [prepared sample]
mapM_ putStrLn $
(showResult <$> [isPalindrome, isPal])
<*> fmap
prepared
[ "",
"a",
"ab",
"aba",
"abba",
sample = "In girum imus nocte et consumimur igni"
]
 
sampleprepared :: String -> String
prepared cs = [toLower c | c <- cs, ' ' /= c]</lang>
sample = "In girum imus nocte et consumimur igni"
 
showResult f s = (show s) <> " -> " <> show (f s)</lang>
prepared :: String -> String
prepared cs = [toLower c | c <- cs, ' ' /= c]</lang>
{{Out}}
<pre>["" -> True,True]</pre>
"a" -> True
"ab" -> False
"aba" -> True
"abba" -> True
"ingirumimusnocteetconsumimurigni" -> True
"" -> True
"a" -> True
"ab" -> False
"aba" -> True
"abba" -> True
"ingirumimusnocteetconsumimurigni" -> True</pre>
 
 
9,655

edits