Haskell -- more type inference problems
I have the following expression:
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)))
and i get the following error:
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)))
yet when i do a :t [0,0,0,0,0,0,0,0,0,0]
开发者_如何转开发 i get back [0,0,0,0,0,0,0,0,0,0] :: (Num t) => [t]
. So why can't i use it in the first expression?
You're using digitToInt
, which returns an Int, not the input type.
Chuck is right. To avoid cluttering your code you can use the .
operator to add the needed function:
(map (singleDigitCount) (map (fromIntegral . digitToInt) (show int)))
This is presuming that singleDigitCount
and processOneCount
also work on arbitrary numeric types.
精彩评论