2017-02-28 153 views
0

我有這樣一個遞歸函數;haskell:遞歸類型不匹配錯誤

elim_all :: Idx -> Idx -> Idx -> Idx -> Mat El -> Mat El 
elim_all c r1b r1e r2 m 
    | r1b == r1e = elim_one c r1b r2 m 
    | otherwise = elim_one c r1b r2 m : elim_all c (r1b+1) r1e r2 m 

elim_one的功能是;

elim_one :: Idx -> Idx -> Idx -> Mat El -> Mat El 
elim_one c r1 r2 m = let val1 = ((m!!r1)!!c) 
         val2 = ((m!!r2)!!c) 
         row1 = (mulr r1 val2 m)!!r1 
         row2 = (mulr r2 val1 m)!!r2 
         nrow = zipWith (-) row1 row2 
         matr = if r1 == r2 
            then m 
            else replacer r1 nrow m 
         in matr 

當我運行它,我得到以下錯誤:

Couldn't match type ‘[El]’ with ‘Int’ 
    Expected type: [El] 
     Actual type: Mat El 
    In the first argument of ‘(:)’, namely ‘elim_one c r1b r2 m’ 
    In the expression: 
     elim_one c r1b r2 m : elim_all c (r1b + 1) r1e r2 m 

錯誤仍然沒有道理給我。我該如何解決這個問題?

+2

'elim_one'的類型是什麼? Mat El是什麼? – Lee

+0

elim_one的類型是elim_one :: Idx - > Idx - > Idx - > Mat El - > Mat El。地毯類型El是[[]]。 – yusuf

+1

那麼'elim_all'的類型不應該是'elem_all :: Idx - > Idx - > Idx - > Idx - > Mat El - > [Mat El]'? – bheklilr

回答

0

因此,這裏是有問題的行:

| otherwise = elim_one c r1b r2 m : elim_all c (r1b+1) r1e r2 m 

現在你已經在你的類型的簽名說,結果*的elim_all將是一個Mat El,但在該行的結果是一個列表(即什麼(:)運營商形成)。

不知道更多關於Mat類型做什麼,我最好的猜測是您需要將此案例的輸出包裝在類型構造函數Mat中。


*功能完全應用時。

1

elim_oneelim_all都計算Mat E1類型的東西。但無論這可能是因爲

(:) :: a -> [a] -> [a] 

,併爲所有類型的X,它認爲x是不一樣的[x]你永遠無法 涉及的elim_oneelim_all評價與(:)操作的結果。