我是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