I'm trying to figure out why my palindrome function isn't working. Does it have to do with how I'm declaring 'w'? I'm attempting to take out all the letters and digits in a string, convert them to uppercase, and places it in a new string 'w'. I attempt to see if they're the same by checking if 'w' is the same as it is reverse
isPalindrome :: [Char] -> Bool
isPalindrome [] = True
isPalindrome [a] = True
isPalindrome (x:xs) | w == reverse w = True
| otherwise = False
where w | isDigit x = x:(isPalindrome xs) -- figured isDigit and isAlpha would be okay to use
| isAlpha (toUpper x) = x:(isPalindrome xs) -- put this under so digits wouldn't have to do toUpper
| otherwise = isPalindrome xs
Obviously I expect the result to return true if it's a palindrome and false otherwise.
The error I'm getting currently is:
* Couldn't match expected type `[Char]' with actual type `Bool'
* In the expression: isPalindrome xs
In an equation for `w':
w | isDigit x = x : (isPalindrome xs)
| isAlpha (toUpper x) = x : (isPalindrome xs)
| otherwise = isPalindrome xs
In an equation for `isPalindrome':
isPalindrome (x : xs)
| w == reverse w = True
| otherwise = False
where
w | isDigit x = x : (isPalindrome xs)
| isAlpha (toUpper x) = x : (isPalindrome xs)
| otherwise = isPalindrome xs
|
48 | | otherwise = isPalindrome xs
|
w
supposed to be? – melpomenex:(isPalindrome xs)
Please explain to your nearest rubber duck what this thing means, in detail. – n. 1.8e9-where's-my-share m.| otherwise = isPalindrome xs
says it's aBool
(because that's whatisPalindrome
returns). – melpomene