title: How to do stateful list operations in haskell
body: <p>I need an operation which iterates over a list and produces a new list, where the new list elements depend on all elements previously seen. To do this I would like to pass an accumulator/state from iteration to iteration.</p>
  
  <p>Think for example of a list of tuples, where the components of a tuple can be "undefined". An undefined value shall assume the latest value of the same component earlier in the list, if any. So at any stage I will have a state of defined components, which I need to pass to the next iteration.</p>
  
  <p>So with a list of type <code>[l]</code> and an accumulator/state of type <code>a</code> there will be a function of type</p>
  
  <pre><code>f :: a -&gt; l -&gt; (a,l)
  </code></pre>
  
  <p>i.e it spits out a new list element and a new accumulator.</p>
  
  <p>Is there a function which allows simply applying f to a list? I looked at fold, scan and unfold, but none of them seem to do the trick.</p>
  
  <p>Edit: While the state monad looks promising, I can only see how I would get the final state, but I fail to see how I would get the new list elements.</p>