[英]Define fromInteger function in Num instance on in Haskell
我試圖做到這一點,以便在控制台中顯示以下內容
1:: 代數圖 Int
將生產
頂點 1
data AlgebraicGraph a
= Empty
| Vertex a
| Overlay (AlgebraicGraph a) (AlgebraicGraph a)
| Connect (AlgebraicGraph a) (AlgebraicGraph a)
instance Num a => Num (AlgebraicGraph a) where
fromInteger x = Vertex x
(+) x y = Overlay x y
(*) x y = Connect x y
但是當我嘗試 fromInteger x = Vertex x 時,我得到的是
Couldn't match type `a' with `Integer'
`a' is a rigid type variable bound by
the instance declaration
at AlgebraicGraph.hs:120:10-40
Expected type: AlgebraicGraph a
Actual type: AlgebraicGraph Integer
* In the expression: Vertex x
In an equation for `fromInteger': fromInteger x = Vertex x
In the instance declaration for `Num (AlgebraicGraph a)'
* Relevant bindings include
fromInteger :: Integer -> AlgebraicGraph a
任何幫助深表感謝!
fromInteger x = Vertex x
問題是x:: Integer
在那里,而不是Num a => a
。 改為這樣做:
fromInteger x = Vertex (fromInteger x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.