Add traversable and foldable instance
This commit is contained in:
parent
628741482b
commit
101b6543bb
@ -16,7 +16,7 @@ show' :: Number -> String
|
|||||||
show' E = "0"
|
show' E = "0"
|
||||||
show' (x:|E) = show (toInteger x)
|
show' (x:|E) = show (toInteger x)
|
||||||
show' (x:|xs) = show (toInteger x) ++ " + 1/(" ++ show' xs ++ ")"
|
show' (x:|xs) = show (toInteger x) ++ " + 1/(" ++ show' xs ++ ")"
|
||||||
show' (M (x:|xs)) = "-" ++ show (toInteger x) ++ " - 1/(" ++ show' xs ++ ")"
|
show' (M (x:|xs)) = '-' : show (toInteger x) ++ " - 1/(" ++ show' xs ++ ")"
|
||||||
|
|
||||||
|
|
||||||
-- Conversion --
|
-- Conversion --
|
||||||
|
@ -10,6 +10,16 @@ instance Functor Continued where
|
|||||||
fmap f (M x) = M (fmap f x)
|
fmap f (M x) = M (fmap f x)
|
||||||
fmap f (x:|xs) = f x :| fmap f xs
|
fmap f (x:|xs) = f x :| fmap f xs
|
||||||
|
|
||||||
|
instance Foldable Continued where
|
||||||
|
foldr f z E = z
|
||||||
|
foldr f z (M x) = foldr f z x
|
||||||
|
foldr f z (x:|xs) = f x (foldr f z xs)
|
||||||
|
|
||||||
|
instance Traversable Continued where
|
||||||
|
traverse _ E = pure E
|
||||||
|
traverse f (M x) = traverse f x
|
||||||
|
traverse f (x:|xs) = (:|) <$> f x <*> traverse f xs
|
||||||
|
|
||||||
instance Num Number where
|
instance Num Number where
|
||||||
(+) = operator (0, 1, 1, 0, 1, 0, 0, 0)
|
(+) = operator (0, 1, 1, 0, 1, 0, 0, 0)
|
||||||
(-) = operator (0, 1, -1, 0, 1, 0, 0, 0)
|
(-) = operator (0, 1, -1, 0, 1, 0, 0, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user