2010-01-14 13 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))) 

을하고 난 다음 오류 얻을 : 나는 :t [0,0,0,0,0,0,0,0,0,0] 내가 다시 [0,0,0,0,0,0,0,0,0,0] :: (Num t) => [t] 얻을 수행 할 때 아직

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))) 

합니다. 그렇다면 첫 번째 표현에서 왜 그것을 사용할 수 없습니까?

답변

4

digitToInt을 사용 중입니다. 입력 유형이 아닌 Int를 반환합니다.

+0

오. 내가 int 타입을 고수해야 하는가 아니면 출력을 Num로 강요 할 수있는 방법이 있을까요? – RCIX

+3

'fromIntegral :: (Integral a, Num b) => a -> b' – ephemient

+0

도움 주셔서 감사합니다 :) – RCIX

0

척이 맞습니다. 코드의 혼란을 방지하기 위해 당신은 필요한 기능을 추가 할 . 연산자를 사용할 수 있습니다 :이 singleDigitCountprocessOneCount는 임의의 숫자 유형에 작동하는지 추정된다

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

.