Search code examples
haskellpalindromestrip

Grammar stripping isPalindrome function for Haskell


Just to make sure there's no confusion.

I am not asking how to write a regular palindrome finder.

I know that would be a repeat question.

I know that to find a palindrome you use

isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = xs == (reverse xs)

This would return true for "madamimadam".

What I can't figure out is the function that would return true for "Madam I'm Adam".


Solution

  • import Data.Char (isAlphaNum, toLower)
    
    isPalindromeStripped :: String -> Bool
    isPalindromeStripped xs = isPalindrome (strip xs)
    
    strip :: String -> String
    strip xs = map toLower (filter isAlphaNum xs)
    

    This version uses strip to turn "Madam I'm Adam" into "MadamImAdam" first, then lowercasing it to "madamimadam".