简体   繁体   English

haskell:递归中的类型不匹配错误

[英]haskell: type mismatch error in recursion

I have such a recursive function; 我有这样的递归函数;

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 function is; 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

When I run it, I get the following error: 运行它时,出现以下错误:

    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

error still doesn't make sense to me. 错误对我来说仍然没有意义。 How can I fix the problem? 我该如何解决该问题?

So here is the line in question: 所以这是有问题的行:

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

Now you have said in your type signature that the result* of elim_all will be a Mat El , but in this line the result is a list (that is what the (:) operator forms). 现在,您已经在类型签名中说过, elim_all的结果*将是Mat El ,但是在这一行中,结果是一个列表(即(:)运算符的形式)。

Without knowing more about what the Mat type does, my best guess is that you need to wrap the output of this case in a Type Constructor of Mat . 在不了解Mat类型的作用的情况下,我的最佳猜测是您需要将此案例的输出包装在Mat的类型构造器中。


* When the function is fully applied. *当功能完全使用时。

Both elim_one and elim_all compute something of type Mat E1 . elim_oneelim_all计算Mat E1类型的东西。 But whatever this might be, since 但是无论如何,因为

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

and for all types x, it holds that x is not the same as [x] you can never relate the results of evaluation of elim_one and elim_all with the (:) operator. 并且对于所有类型x,它都认为x[x]您永远无法将elim_oneelim_all的求值结果与(:)运算符相关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM