【发布时间】:2011-04-17 04:29:13
【问题描述】:
下面以digits 开头的代码返回一个整数列表。以altSumOfDigits 开头的代码应该返回相同的列表,但 Prelude 却在抱怨类型错误,我现在不明白。
Couldn't match expected type `a' against inferred type `Integer'
digits' :: Integer -> [Integer]
digits' 0 = []
digits' x = (x `mod` 10) : digits' (x `div` 10)
digits :: Integer -> [Integer]
digits 0 = [0]
digits x = reverse $ digits' x
altSumOfDigits :: Integer -> [a]
altSumOfDigits num = [ x | x <- (digits num)]
(我知道altSumOfDigits num = [ x | x <- (digits num)] 在这种形式下相当无用。稍后我将使用 if 表达式和对单个整数的操作来扩展它的功能。)
任何解释为什么这不起作用?
【问题讨论】:
标签: list function haskell types