Add traversable and foldable instance

This commit is contained in:
rnhmjoj 2015-06-01 18:21:24 +02:00
parent 628741482b
commit 101b6543bb
2 changed files with 11 additions and 1 deletions

View File

@ -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 --

View File

@ -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)