From our lecture notes:
get' state = (state, state)
put' item state = ((), item) -- () is void value
data State s a = State (s -> (a, s))
-- Functions get and put: -- (sic!)
get :: State s s
get = State get'
put :: s -> State s ()
put item = State (put' item)
I am totally lost in these two functions get
and put
.
First, there is no arrow in the type signature of get
:
get :: State s s
What does it mean?
What does s
mean in both get
and put
? Are they state?
Remember that State s a
is essentially a function s -> (a, s)
, that is, a function that takes a state and returns a value of some type a
and a new state.
So get :: State s s
is a s -> (s, s)
, a function that simply returns the current state.
put :: s -> s -> ((), s)
is implemented as s -> _ -> ((), s)
, a function that takes a given state, ignores the current state, returns the given state, and produces no new values.