title: How can I keep track of two lists in a function (fold) in OCaml?
body: <p>The goal of my this code is two split one list into two lists based on a simple condition and I'm having trouble doing this in just one function. </p>
  
  <p>Currently, I fold over the list passed in and say if the condition is true, add to list one. Then I fold again and say if the condition is false, add to list two. This is horribly inefficient for larger lists and I don't know how to combine it into one fold.</p>
  
  <p>My working, inefficient code currently looks like this:</p>
  
  <pre><code>let function test old_list_one original_list =
    let list_one = (fold (fun a elt -&gt; 
        if condition = true then elt::a
          else a) old_list_one original_list) in
    let list_two = (fold (fun a elt -&gt; 
        if condition = false then elt::a
              else a) [] original_list) in
  do_something list_one list_two
  </code></pre>
  
  <p>This works for smaller sets but runs out of resources on larger sets. I tried combining the folds like this:</p>
  
  <pre><code>let function test old_list_one = 
    let list_two = [] in
    let list_one = (fold (fun a elt -&gt; 
        if (condition) = true then elt::a
          else elt::list_two) old_list_one original) in
  do_something list_one list_two
  </code></pre>
  
  <p>OCaml doesn't keep track of list_two and it remains []...how can I can keep track of list_two and list_one in ONE function?</p>
  
  <p>Also, for those unfamiliar with fold - it's a function that iterates over every element in original and accumulates the result to old_list_one (in this example)</p>
  
  <p>Edit: Using a tuple as the accumulator did the trick!</p>