Skip to content

Commit 3c715a8

Browse files
committed
Add State exercises
1 parent 8a9caa9 commit 3c715a8

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

chapter-23/try-random/app/Main.hs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ intToDie x
2424
|x == 6 = DieSix
2525
|otherwise = error $ "intToDie got non 1-6 integer: " ++ show x
2626

27-
main :: IO ()
28-
main = undefined
29-
3027
rollDiceThreeTimes :: (Die, Die, Die)
3128
rollDiceThreeTimes = do
3229
let s = mkStdGen 0
@@ -102,3 +99,33 @@ instance Monad (Moi s) where
10299
let (a, _) = f x
103100
Moi (gb) = g a
104101
in gb x
102+
103+
main = undefined
104+
105+
{-exercises-}
106+
{-ex1-}
107+
get :: Moi s s
108+
get = Moi $ \x -> (x, x)
109+
{-usage is-}
110+
{-(put "blah") "woot"-}
111+
112+
{-ex2-}
113+
put :: s -> Moi s ()
114+
put s = Moi $ \_ -> ((), s)
115+
116+
{-usage is-}
117+
{-(put "blah") "woot"-}
118+
119+
{-ex3-}
120+
exec :: Moi s a -> s -> s
121+
exec (Moi sa) = snd . sa
122+
123+
{-ex4-}
124+
eval :: Moi s a -> s -> a
125+
eval (Moi sa) = fst . sa
126+
127+
{-ex5-}
128+
modify :: (s -> s) -> Moi s ()
129+
modify f = (Moi $ \s -> ((), f s))
130+
131+
ex5 = runMoi (Main.modify (+1) >> Main.modify (+1)) 0

0 commit comments

Comments
 (0)