我在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]
我在网上搜索并发现了很多类似的问题,但似乎没有什么能接近我的问题。如何使这段代码有效?
答案 0 :(得分:10)
数据类型的类约束don't mean anything.
删除数据类型的约束。
data Mat a = Mat {nexp :: Int, mat :: QT a}
deriving (Eq, Show)