title: Creation/Handling of lists of Monad.State's?
body: <p>I'm just in the process of learning haskell and I am sure that an elegant solution to the following problem exists.
  Given a function f that returns a stateful computation</p>
  
  <pre><code>f :: (Num a) =&gt; a -&gt; a -&gt; State [a] a
  f x y = modify ((2*x):) &gt;&gt; return (x+y)   -- state and value are modified based on the passed values
  </code></pre>
  
  <p>I am looking for the cleanest way to generate multiple <code>State</code>'s from a given one recursively (as shown below). One solution I came up with is the following</p>
  
  <pre><code>g :: (Num a) =&gt; [a] -&gt; (a, [a]) -&gt; [(a,[a])]
  g [] _ = []
  g (x:xs) aa@(a,b) = (new : next)
      where new = runState (f x a) b   -- here the old value a is required!
            next = g xs aa
  </code></pre>
  
  <p>but I guess something like </p>
  
  <pre><code>g :: [a] -&gt; [State [a] a] 
  </code></pre>
  
  <p>should be possible and cleaner? I didn't succeed trying this and get errors from StateT that I can't figure out.</p>
  
  <p>Thank you!</p>
  
  <p><em>Background:</em> The code is a simplification of parts of a graph generator I am writing, where the state is the current adjacency vector. For each node, several edges may be created and therefore multiple state is required representing the different (partial) graphs.</p>
  
  <p><em>Edit</em>: (Try of) A description in words of the above function <code>g x (y, s)</code> in words:</p>
  
  <p>For a given list of values <code>x</code>, a single value <code>y</code> and a state <code>s</code>, compute recursively for each <code>x_i</code> in <code>x</code> a new value and state from <code>(y,s)</code> given by the stateful computation <code>f x_i y</code>  and return the result as a list.  </p>
  
  <p><em>Edit 2</em>: Example output:</p>
  
  <pre><code> g [1,2,3] (4,[2,3,4]) == [(5,[10,2,3,4]),(6,[12,2,3,4]),(7,[14,2,3,4])]
  </code></pre>