Skip to content

Commit 5b9e017

Browse files
committed
Complete chapter 12 exercises
1 parent 283893c commit 5b9e017

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

12-monads-and-more/12.05-exercises.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,24 @@ instance Monad Expr where
6868

6969
-----------------------------------------------------------------------------
7070
-- 8.
71+
type State = Int
7172

73+
newtype ST a = S (State -> (a, State))
74+
75+
instance Functor ST where
76+
fmap f st = do
77+
x <- st
78+
S $ \s -> (f x, s)
79+
80+
instance Applicative ST where
81+
pure x = S $ \s -> (x, s)
82+
stf <*> stx = do
83+
f <- stf
84+
x <- stx
85+
return $ f x
86+
87+
instance Monad ST where
88+
S st >>= f = S $ \s ->
89+
let (x, s') = st s
90+
(S st') = f x
91+
in st' s'

0 commit comments

Comments
 (0)