haskell - Creating a `State [Int] Int` -
reading through learn haskell, i'm trying construct stack [int] int
:
ghci>import control.monad.state ghci> let stack = state ([1,2,3]) (1) :: state [int] int <interactive>:58:20: couldn't match expected type `s0 -> (state [int] int, s0)' actual type `[t0]' in first argument of `state', namely `([1, 2, 3])' in expression: state ([1, 2, 3]) (1) :: state [int] int in equation `stack': stack = state ([1, 2, 3]) (1) :: state [int] int
how can create stack [int] int
?
it depends on you're trying do. state s a
newtype
kind of function type (specifically s -> (a, s)
), doesn't make sense make state
value list. simplified (internal) definition of state
looks like
newtype state s = state { runstate :: s -> (a, s) }
though won't use state
constructor directly, illustrate fact state s a
value consists of function.
you need function updates state in way (which considered "state
action"), can use runstate :: state s -> s -> (a, s)
execute provided state
action, given initial state (the s
argument).
it looks want use [1, 2, 3]
initial state, need provide update function (which use construct state s a
value itself).
in learn haskell example, stack
type synonym represents actual stack data while state stack ...
represents stateful action on stack
data. instance, action of type state stack int
uses stack
value state , results in int
when executed.
Comments
Post a Comment