2
我在Haskell這一段代碼,拒絕編譯:無法推斷(顯示T)
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]
我在網上搜索,發現了很多類似的問題,但似乎並沒有接近我的問題。我怎樣才能使這個代碼工作?關於數據類型的
你不應該對數據類型使用約束:http://stackoverflow.com/questions/12770278/typeclass-constraints-on-data-declarations –