我有以下表达式:
getCount :: (Num a) => a -> [a]
getCount int = foldl
processOneCount
[0,0,0,0,0,0,0,0,0,0]
(map (singleDigitCount) (map (digitToInt) (show int)))
我收到以下错误:
Couldn't match expected type `a' against inferred type `Int'
`a' is a rigid type variable bound by
the type signature for `getCount'
at C:\Users\RCIX\Desktop\Haskell Code\test.hs:23:17
Expected type: [a]
Inferred type: [Int]
In the expression:
foldl
processOneCount
[0, 0, 0, 0, ....]
(map (singleDigitCount) (map (digitToInt) (show int)))
In the definition of `getCount':
getCount int
= foldl
processOneCount
[0, 0, 0, ....]
(map (singleDigitCount) (map (digitToInt) (show int)))
当我做:t [0,0,0,0,0,0,0,0,0,0]
时,我会回来[0,0,0,0,0,0,0,0,0,0] :: (Num t) => [t]
。那么为什么我不能在第一个表达式中使用它呢?
答案 0 :(得分:4)
你正在使用digitToInt
,它返回一个Int,而不是输入类型。
答案 1 :(得分:0)
查克是对的。为避免使代码混乱,您可以使用.
运算符添加所需的函数:
(map (singleDigitCount) (map (fromIntegral . digitToInt) (show int)))
这假设singleDigitCount
和processOneCount
也适用于任意数字类型。