弄清楚一个数字是否是回文

时间:2017-06-02 18:47:01

标签: haskell

我在Haskell上比较新,我不知道为什么不能编译。我要做的是创建一个函数,通过创建一个辅助函数来检查字符串形式的数字是否是回文。谢谢!

isPalindrome :: [Char] -> Bool
isPalindrome s = (helper . Char.digitToInt) s
    where helper [] = True
          helper [x] = True
          helper (x:xs) = x == (last xs) && (helper . init) xs

错误:

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

1 个答案:

答案 0 :(得分:3)

它无法编译,因为digitToInt需要Char并返回Int

您给它一个列表(s :: [Char])并将结果视为另一个列表(helper列出一个列表)。