зеркало из
https://github.com/iharh/notes.git
synced 2025-10-30 05:06:05 +02:00
25 строки
635 B
Plaintext
25 строки
635 B
Plaintext
https://www.fpcomplete.com/school/starting-with-haskell/basics-of-haskell/12-State-Monad
|
|
|
|
State Monad:
|
|
|
|
newtype State s a = State { runState :: (s -> (a,s)) }
|
|
|
|
instance Monad (State s) where
|
|
return a = State $ \s -> (a,s)
|
|
(State x) >>= f = State $ \s -> let (v,s1) = x s in runState (f v) s1
|
|
|
|
class MonadState m s | m -> s where
|
|
get :: m s
|
|
put :: s -> m ()
|
|
|
|
instance MonadState (State s) s where
|
|
get = State $ \s -> (s,s)
|
|
put s = State $ \_ -> ((),s)
|
|
|
|
instance MonadPlus (State s) where
|
|
mzero = ...
|
|
mplus = ...
|
|
|
|
http://stackoverflow.com/questions/5541828/haskell-state-monad-and-monadic-guard
|
|
|