2
我宣佈我的數據是這樣的:哈斯克爾:數據類型
data Op = Plus | Minus | Mul | Div | Pow
deriving (Eq, Show)
type Name = String
data Variable a = Variable Name (Expression a)
deriving (Eq, Show)
data Declaration a = Declaration (Variable a)
deriving (Eq, Show)
{- The core symbolic manipulation type -}
data Expression a =
Number a -- Simple number, such as 5
| Expression Op (Expression a) (Expression a)
deriving (Eq, Show)
在GHCI,我想通過鍵入創建宣言的一個實例: Declaration Variable "var1" 2+3
,但它不工作,我想這是隻是一個錯誤的語法,但我不知道如何。
另外我想知道什麼時候需要使用實例?這是我從一本書上得到了代碼:給
(Declaration Variable "var1" 2) + 3
instance Num a => Num (Expression a) where
a + b = Expression Plus a b
a - b = Expression Minus a b
a * b = Expression Mul a b
negate a = Expression Mul (Number (-1)) a
abs a = error "abs is unimplemented"
signum _ = error "signum is unimplemented"
fromInteger i = Number (fromInteger i)