定义循环类型的Integers
表示以像这样工作:
data Integers = Zero | Next Integers | Prev Integers
并制作这个表示,类 Num 的实例,这意味着你应该可以(+), (*), (==), abs, signum, show
使用Integers
直到现在我这样定义:
data Integers = Zero | Integers Int deriving (Show)
next :: Integers -> Integers
next Zero = Integers 1
next (Integers a) = Integers a + Integers 1
prev :: Integers -> Integers
prev (Integers 1) = Zero
prev (Integers a) = Integers a - Integers 1
instance Eq Integers where
Zero == Zero = True
Integers a == Integers b = a == b
_ == _ = False
instance Num Integers where
Integers a + Integers b = Integers (a + b)
Integers a - Integers b = Integers (a - b)
Integers a * Integers b = Integers (a * b)
abs (Integers a) = Integers (abs a)
signum (Integers a) = Integers (signum a)
fromInteger a = Integers (fromInteger a)
但不符合data Integers = Zero | Next Integers | Prev Integers
预期