[英]Figuring out if a number is a palindrome
I'm relatively new at Haskell and I have no idea why this won't compile. 我在Haskell相对较新,我不知道为什么它无法编译。 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
. 它不会编译,因为
digitToInt
接受一个Char
并返回一个Int
。
You're giving it a list ( s :: [Char]
) and treating the result as another list ( helper
takes a list). 您给它一个列表(
s :: [Char]
),并将结果视为另一个列表( helper
获取一个列表)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.