From 423693811904a6038a1a7532700726799e05fade Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Fri, 8 Aug 2014 09:20:42 +0800 Subject: add difflist --- difflist.hs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 difflist.hs diff --git a/difflist.hs b/difflist.hs new file mode 100644 index 0000000..f206351 --- /dev/null +++ b/difflist.hs @@ -0,0 +1,27 @@ +import Control.Monad.Writer + +newtype DiffList a = DiffList { getDiffList :: [a] -> [a] } + +toDiffList :: [a] -> DiffList a +toDiffList xs = DiffList (xs++) + +fromDiffList :: DiffList a -> [a] +fromDiffList (DiffList f) = f [] + +instance Monoid (DiffList a) where + mempty = DiffList (\xs -> [] ++ xs) + (DiffList f) `mappend` (DiffList g) = DiffList (\xs -> f (g xs)) + +diffCountDown :: Int -> Writer (DiffList String) () +diffCountDown 0 = do + tell (toDiffList ["0"]) +diffCountDown x = do + diffCountDown (x-1) + tell (toDiffList [show x]) + +normCountDown :: Int -> Writer [String] () +normCountDown 0 = do + tell ["0"] +normCountDown x = do + normCountDown (x-1) + tell [show x] -- cgit v1.2.3-70-g09d2