3

我在 Haskell 中有这段代码拒绝编译:

data (Eq a, Num a, Show a) => Mat a = Mat {nexp :: Int, mat :: QT a}
 deriving (Eq, Show)

data (Eq a , Show a ) => QT a = C a | Q (QT a ) (QT a ) (QT a ) (QT a )
 deriving (Eq, Show)


cs:: (Num t) => Mat t -> [t]

cs(Mat nexp (Q a b c d)) =(css (nexp-1) a c)++(css (nexp-1) b d)
    where
        css 0 (C a) (C b) = (a-b):[]
        css nexp (Q a b c d) (Q e f g h) = (zipWith (+) (css (nexp-1) a c) (css (nexp-1) e g))++(zipWith (+)(css (nexp-1) b d) (css (nexp-1) f h))

我有这个错误:

Could not deduce (Show t) arising from a use of `Mat'
from the context (Num t)
bound by the type signature for cs:: Num t => Mat t -> [t]

我在网上搜索并发现了许多类似的问题,但似乎没有什么能接近我的问题。我怎样才能使这段代码工作?

4

1 回答 1

10

数据类型的类约束没有任何意义。

请参阅此相关问题

删除对数据类型的约束。

data Mat a = Mat {nexp :: Int, mat :: QT a}
    deriving (Eq, Show)
于 2013-07-31T13:35:10.670 回答