forked from cyga/real-world-haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleState.hs
39 lines (30 loc) · 980 Bytes
/
SimpleState.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
37
38
39
-- file: ch14/SimpleState.hs
type SimpleState s a = s -> (a, s)
-- file: ch14/SimpleState.hs
type StringState a = SimpleState String a
-- file: ch14/SimpleState.hs
returnSt :: a -> SimpleState s a
returnSt a = \s -> (a, s)
-- file: ch14/SimpleState.hs
returnAlt :: a -> SimpleState s a
returnAlt a s = (a, s)
-- file: ch14/SimpleState.hs
bindSt :: (SimpleState s a) -> (a -> SimpleState s b) -> SimpleState s b
bindSt m k = \s -> let (a, s') = m s
in (k a) s'
-- file: ch14/SimpleState.hs
-- m == step
-- k == makeStep
-- s == oldState
bindAlt step makeStep oldState =
let (result, newState) = step oldState
in (makeStep result) newState
-- file: ch14/SimpleState.hs
bindAlt :: (s -> (a, s)) -- step
-> (a -> s -> (b, s)) -- makeStep
-> (s -> (b, s)) -- (makeStep result) newState
-- file: ch14/SimpleState.hs
getSt :: SimpleState s s
getSt = \s -> (s, s)
putSt :: s -> SimpleState s ()
putSt s = \_ -> ((), s)