I am just studying haskell and I am sure that there is an elegant solution to the following problem. Given a function f that returns a stateful calculation
f :: (Num a) => a -> a -> State [a] a fxy = modify ((2*x):) >> return (x+y) -- state and value are modified based on the passed values
I am looking for the cleanest way to generate multiple State from a given recursively (as shown below). One solution I came up with is the following
g :: (Num a) => [a] -> (a, [a]) -> [(a,[a])] g [] _ = [] g (x:xs) aa@ (a,b) = (new : next) where new = runState (fxa) b -- here the old value a is required! next = g xs aa
but I think something like
g :: [a] -> [State [a] a]
should be possible and cleaner? I was not able to do this and get errors from StateT that I cannot understand.
Thanks!
Background: code is a simplification of the parts of the graph generator that I write, where the state is the current adjacency vector. For each node, several edges can be created, and therefore several states are required that represent different (partial) graphs.
Edit: (Try it) Description in words of the above function gx (y, s) in words:
For a given list of values โโof x , a single value of y and state s , derive recursively for each x_i into x new value and state from (y,s) , the calculation function f x_i y specified by the state function, and return the result as a list.
Edit 2: Example output:
g [1,2,3] (4,[2,3,4]) == [(5,[10,2,3,4]),(6,[12,2,3,4]),(7,[14,2,3,4])]