From 101b6543bb59c87b1e0c082e5e7a34dd2fc7b266 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Mon, 1 Jun 2015 18:21:24 +0200 Subject: [PATCH] Add traversable and foldable instance --- src/Data/Number/Functions.hs | 2 +- src/Data/Number/Instances.hs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Data/Number/Functions.hs b/src/Data/Number/Functions.hs index 459775b..7688395 100644 --- a/src/Data/Number/Functions.hs +++ b/src/Data/Number/Functions.hs @@ -16,7 +16,7 @@ show' :: Number -> String show' E = "0" show' (x:|E) = show (toInteger x) 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 -- diff --git a/src/Data/Number/Instances.hs b/src/Data/Number/Instances.hs index 392562b..9bca10d 100644 --- a/src/Data/Number/Instances.hs +++ b/src/Data/Number/Instances.hs @@ -10,6 +10,16 @@ instance Functor Continued where fmap f (M x) = M (fmap f x) 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 (+) = operator (0, 1, 1, 0, 1, 0, 0, 0) (-) = operator (0, 1, -1, 0, 1, 0, 0, 0)