-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWriter.hs
36 lines (27 loc) · 925 Bytes
/
Writer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Control.Monad.Writer
logNumber :: Int -> Writer [String] Int
logNumber x = writer (x, ["Got number: " ++ show x])
newtype DiffList a = DiffList {getDiffList :: [a] -> [a]}
instance Monoid (DiffList a) where
mempty = toDiffList []
mappend (DiffList f) (DiffList g) = DiffList (\xs -> f (g xs))
toDiffList :: [a] -> DiffList a
toDiffList xs = DiffList (xs ++ )
fromDiffList :: DiffList a -> [a]
fromDiffList (DiffList f) = f []
countdownDiff :: Int -> Writer (DiffList String) ()
countdownDiff 0 = do
tell (toDiffList ["0"])
countdownDiff x = do
countdownDiff (x-1)
tell (toDiffList [show x])
countdown :: Int -> Writer [String] ()
countdown 0 = do
tell ["0"]
countdown x = do
countdown (x-1)
tell [show x]
diffTest :: Int -> IO ()
diffTest = mapM_ putStrLn . fromDiffList . snd . runWriter . countdownDiff
test :: Int -> IO ()
test = mapM_ putStrLn . snd . runWriter . countdown