haskell:递归类型不匹配错误
问题描述:
我有这样一个递归函数;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
错误仍然没有道理给我。我该如何解决这个问题?
答
因此,这里是有问题的行:
| otherwise = elim_one c r1b r2 m : elim_all c (r1b+1) r1e r2 m
现在你已经在你的类型的签名说,结果*的elim_all
将是一个Mat El
,但在该行的结果是一个列表(即什么(:)
运营商形成)。
不知道更多关于Mat
类型做什么,我最好的猜测是您需要将此案例的输出包装在类型构造函数Mat
中。
*功能完全应用时。
答
elim_one
和elim_all
都计算Mat E1
类型的东西。但无论这可能是因为
(:) :: a -> [a] -> [a]
,并为所有类型的X,它认为x
是不一样的[x]
你永远无法 涉及的elim_one
和elim_all
评价与(:)
操作的结果。
'elim_one'的类型是什么? Mat El是什么? – Lee
elim_one的类型是elim_one :: Idx - > Idx - > Idx - > Mat El - > Mat El。地毯类型El是[[]]。 – yusuf
那么'elim_all'的类型不应该是'elem_all :: Idx - > Idx - > Idx - > Idx - > Mat El - > [Mat El]'? – bheklilr