我是Haskell世界的新手,所以這可能是一個基本問題。Haskell - 在構造函數中推斷類型
難道這代碼:
(Monomio (Numero (Integer 15)) (Integer 20))
:
data Numero =
Integer Integer |
Rational Rational |
Double Double
deriving (Show)
data Elemento =
Numero Numero |
Incognita String
deriving (Show)
data Monomio = Monomio {base :: Elemento, exp :: Numero} deriving(Show)
main = print (Monomio (Numero (Integer 15)) (Integer 20))
在沒有明確的類型的表達?
該表達式:
main = print (Monomio (Integer 15) (Integer 20))
其是較短的是不模糊的,如(整數15)不適合(根結線蟲字串)的定義,但它不編譯:
main.hs:13:24:
Couldn't match expected type `Elemento' with actual type `Numero'
In the first argument of `Monomio', namely `(Integer 15)'
In the first argument of `print', namely
`(Monomio (Integer 15) (Integer 20))'
爲什麼?
我可以看到'instance Num Numero where fromInteger = Integer; [...]',但你會如何爲Elemento創建一個實例? – Bergi
*我會使用NumElemento和VarElemento或類似的東西。*你的意思是將'Incognita String'連接到** Numero **構造函數嗎? – lilezek
我的意思是我會使用'NumElemento'來表示我爲'變量'構造了'Elemento','VarElemento'類型的東西 - 你稱之爲'Incognita'。 – epsilonhalbe