I'm trying so hard to wrap my head around the State Monad, and I do not understand the following:
Given the implementation of return
and (>>=)
, when you say State $ \s ->....
, where does s
come from? I mean, when you start performing >>= ... >>=
, doesn't it mean that somewhere in your beginning of the chain you somehow have to provide for that initial parameter?
newtype State s a=State { runState::s->(a,s) }
instance Monad (State s) where
return a=State $ \s->(a,s)
(>>=) m g=State $ \s -> let (a,s')= runState m s in
runState (g a) s'
In (>>=)
you say State $ \s -> runState m s
, and I do not get when is that initial (\s -> ...) argument
(with a REAL argument
) called?
Can someone explain, please?
Later Edit:
Can someone show me how would the initial
state be set, let's say if it needs to get a value using getLine
?
main::IO()
main=do
argument<-getLine
--how do i set initial state with argument?
m >> f1 >> f2 >> f3