It depends on what you're trying to do. State s a
is essentially a newtype
for a certain kind of function type (specifically s -> (a, s)
), so it doesn't really make sense to make a State
value from just a list. The simplified (internal) definition of State
looks something like
newtype State s a = State { runState :: s -> (a, s) }
Though you won't use the State
constructor directly, it does illustrate the fact that a State s a
value consists of a function.
You need a function that updates the state in some way (which could be considered a "State
action"), then you can use runState :: State s a -> s -> (a, s)
to execute the provided State
action, given a certain initial state (the s
argument).
It looks like you want to use [1, 2, 3]
as your initial state, but you do also need to provide that update function (which is what you use to construct the State s a
value itself).
In the Learn You a Haskell example, the Stack
type synonym represents the actual stack data while State Stack ...
represents a stateful action on the Stack
data. For instance, an action of type State Stack Int
uses a Stack
value as its state and results in an Int
when it is executed.