[英]Figuring out if a number is a palindrome
我在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
它不會編譯,因為digitToInt
接受一個Char
並返回一個Int
。
您給它一個列表( s :: [Char]
),並將結果視為另一個列表( helper
獲取一個列表)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.