I'm relatively new at Haskell and I have no idea why this won't compile. What I'm trying to do is create a function that checks if a number in string form is a palindrome by creating a helper function that does that. Thanks!
isPalindrome :: [Char] -> Bool
isPalindrome s = (helper . Char.digitToInt) s
where helper [] = True
helper [x] = True
helper (x:xs) = x == (last xs) && (helper . init) xs
Errors:
euler.hs:29:28: error:
• Couldn't match type ‘Int’ with ‘[a0]’
Expected type: Char -> [a0]
Actual type: Char -> Int
• In the second argument of ‘(.)’, namely ‘Char.digitToInt’
In the expression: helper . Char.digitToInt
In the expression: (helper . Char.digitToInt) s
euler.hs:29:45: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the first argument of ‘helper . Char.digitToInt’, namely ‘s’
In the expression: (helper . Char.digitToInt) s
In an equation for ‘isPalindrome’:
isPalindrome s
= (helper . Char.digitToInt) s
where
helper [] = True
helper [x] = True
helper (x : xs) = x == (last xs) && (helper . init) xs
It doesn't compile because digitToInt
takes a Char
and returns an Int
.
You're giving it a list ( s :: [Char]
) and treating the result as another list ( helper
takes a list).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.