List 无法从算术序列推断枚举a
我是Haskell的新手,在尝试编译此代码(计算行列式的一部分)时遇到了无法解决的错误:List 无法从算术序列推断枚举a,list,haskell,enums,List,Haskell,Enums,我是Haskell的新手,在尝试编译此代码(计算行列式的一部分)时遇到了无法解决的错误: 谢谢您的帮助。您可能想交换foldr调用的参数,因此: 致: (一些额外的修改) 出现错误的原因是,作为foldr的结果,您希望出现[[a]]]。因此,Haskell认为[0..genericLength x-1]也应该有[[a]]]类型。由于它内部有一个。结构,因此它派生出0和genericLength x-1应该具有类型[a],但它不知道如何枚举该类型。然而,上述所有推理都基于传递给foldr的错误参
谢谢您的帮助。您可能想交换
foldr
调用的参数,因此:
致:
(一些额外的修改)
出现错误的原因是,作为foldr
的结果,您希望出现[[a]]]
。因此,Haskell认为[0..genericLength x-1]
也应该有[[a]]]
类型。由于它内部有一个。
结构,因此它派生出0
和genericLength x-1
应该具有类型[a]
,但它不知道如何枚举该类型。然而,上述所有推理都基于传递给foldr
的错误参数
我也认为我们根本不需要genericLength
。我们可以将length
与Int
一起使用,因此:
--pivotzeile = 1
--pivotspalte 0 based!
genLaplaceMatrix :: (Num a, Enum a) => [[a]] -> Int -> [[a]]
genLaplaceMatrix [[]] _ = error "Cannot calculate LaplaceMatrix from [[]]"
genLaplaceMatrix x pivotSpalte
| pivotSpalte < 0 = error "invalid column"
| pivotSpalte > ((genericLength x) -1) = error "invalid column"
| otherwise = foldr (\y -> (conVecs y:)) [] [0..length x-1]
where
leftPart = take pivotSpalte
rightPart lP z = takeRight (leftPart lP) z
conVecs col = concatVectors (leftPart x!!col) (rightPart (leftPart x!!col) (x!!col) )
(假设你计算了行列式)这是正确的答案。(我最近问了一个类似的问题,但删除了它,因为问题的表述不正确,讨论偏离了主题。)请不要删除问题。格式可以修改。下次我会记住这一点;但问题也有了一些变化,这就是为什么我认为最好打开一个新的话题。@FabianSchneider:你为什么要使用
genericLength
?Int
的最小范围为-2^29到+2^29,这似乎足够了。我自己通过在声明中使用[[a]]->Int->[[a]]解决了第二个错误。非常感谢:D
main.hs:29:76: error:
• Could not deduce (Enum [a])
arising from the arithmetic sequence ‘0 .. ((genericLength x) - 1)’
from the context: (Num a, Enum a, Num b, Ord b)
bound by the type signature for:
genLaplaceMatrix :: (Num a, Enum a, Num b, Ord b) =>
[[a]] -> b -> [[a]]
at main.hs:24:1-72
• In the second argument of ‘foldr’, namely
‘[0 .. ((genericLength x) - 1)]’
In the expression:
foldr
(\ y acc -> (conVecs y) : acc) [0 .. ((genericLength x) - 1)] []
In an equation for ‘genLaplaceMatrix’:
genLaplaceMatrix x pivotSpalte
| pivotSpalte < 0 = error "invalid column"
| pivotSpalte > ((genericLength x) - 1) = error "invalid column"
| otherwise
= foldr
(\ y acc -> (conVecs y) : acc) [0 .. ((genericLength x) - 1)] []
where
leftPart = take pivotSpalte
rightPart lP z = takeRight (leftPart lP) z
conVecs col
= concatVectors
(leftPart x !! col) (rightPart (leftPart x !! col) (x !! col))
main.hs:29:77: error:
• Could not deduce (Num [a]) arising from the literal ‘0’
from the context: (Num a, Enum a, Num b, Ord b)
bound by the type signature for:
genLaplaceMatrix :: (Num a, Enum a, Num b, Ord b) =>
[[a]] -> b -> [[a]]
at main.hs:24:1-72
• In the expression: 0
In the second argument of ‘foldr’, namely
‘[0 .. ((genericLength x) - 1)]’
In the expression:
foldr
(\ y acc -> (conVecs y) : acc) [0 .. ((genericLength x) - 1)] []
main.hs:31:23: error:
• Couldn't match expected type ‘Int’ with actual type ‘b’
‘b’ is a rigid type variable bound by
the type signature for:
genLaplaceMatrix :: forall a b.
(Num a, Enum a, Num b, Ord b) =>
[[a]] -> b -> [[a]]
at main.hs:24:21
• In the first argument of ‘take’, namely ‘pivotSpalte’
In the expression: take pivotSpalte
In an equation for ‘leftPart’: leftPart = take pivotSpalte
• Relevant bindings include
pivotSpalte :: b (bound at main.hs:26:20)
genLaplaceMatrix :: [[a]] -> b -> [[a]] (bound at main.hs:25:1)
foldr (\y acc -> (conVecs y):acc ) [0..((genericLength x)-1)] []
foldr (\y -> (conVecs y:)) [] [0..genericLength x-1]
--pivotzeile = 1
--pivotspalte 0 based!
genLaplaceMatrix :: (Num a, Enum a) => [[a]] -> Int -> [[a]]
genLaplaceMatrix [[]] _ = error "Cannot calculate LaplaceMatrix from [[]]"
genLaplaceMatrix x pivotSpalte
| pivotSpalte < 0 = error "invalid column"
| pivotSpalte > ((genericLength x) -1) = error "invalid column"
| otherwise = foldr (\y -> (conVecs y:)) [] [0..length x-1]
where
leftPart = take pivotSpalte
rightPart lP z = takeRight (leftPart lP) z
conVecs col = concatVectors (leftPart x!!col) (rightPart (leftPart x!!col) (x!!col) )
> print $ det [[1,2],[3,4]]
-2