4 newtype State s a = State { runState' :: s -> (# a, s #) }
6 instance Monad (State s) where
7 return x = State $ \s -> (# x, s #)
8 m >>= n = State $ \s -> case runState' m s of
9 (# r, s' #) -> runState' (n r) s'
12 get = State $ \s -> (# s, s #)
14 gets :: (s -> a) -> State s a
15 gets f = State $ \s -> (# f s, s #)
17 put :: s -> State s ()
18 put s' = State $ \_ -> (# (), s' #)
20 modify :: (s -> s) -> State s ()
21 modify f = State $ \s -> (# (), f s #)
24 evalState :: State s a -> s -> a
25 evalState s i = case runState' s i of
29 execState :: State s a -> s -> s
30 execState s i = case runState' s i of
34 runState :: State s a -> s -> (a, s)
35 runState s i = case runState' s i of
36 (# a, s' #) -> (a, s')
41 => (acc -> x -> m (acc, y)) -- ^ combining funcction
42 -> acc -- ^ initial state
44 -> m (acc, [y]) -- ^ final state, outputs
46 mapAccumLM _ s [] = return (s, [])
48 = do (s1, x') <- f s x
49 (s2, xs') <- mapAccumLM f s1 xs