2010-01-14 38 views
1

我有以下表现:哈斯克尔 - 更多的类型推断问题

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]。那么为什么我不能在第一个表达式中使用它?

回答

4

您使用的是digitToInt,它返回一个Int,而不是输入类型。

+0

哦。我应该坚持一个int类型,还是有办法强制输出成Num? – RCIX 2010-01-14 22:11:43

+3

'fromIntegral ::(Integral a,Num b)=> a - > b' – ephemient 2010-01-14 22:12:49

+0

感谢您的帮助:) – RCIX 2010-01-14 22:15:41

0

查克是正确的。为了避免弄乱你的代码,你可以使用.运营商添加所需的功能:

(map (singleDigitCount) (map (fromIntegral . digitToInt) (show int))) 

这是假设该singleDigitCountprocessOneCount也任意数字类型的工作。