18
我要定義一個操作(稱之爲+-+
),在這個作品:運算符重載
if a,b are Char => a +-+ b = [a][b]
if a,b are Integer => a +-+ b = a+b
我嘗試:
class Summable a where
(+-+) :: a -> a -> b
instance Summable Integer where
a +-+ b = a + b
instance Summable Char where
a +-+ b = [a] ++ [b]
,但我得到的錯誤:
Couldn't match type `b' with `Integer'....
Couldn't match type `b' with `[Char]' ....
可以做到這一點嗎?怎麼樣?
請注意''[a] ++ [b]'只是'[a,b]'... –