title: Fixing list output in Prolog
body: <p>I'm trying to make a solver for the Dutch National flag problem. Basically, given a list, I want to sort them in the order of Red-White-Blue. Red, White and Blue are defined by their predicates (i.e. red(x), white(x) etc.)
  Currently, I have the following code:</p>
  
  <pre><code>red(1).
  white(2).
  blue(3).
  
  dutch(Xs,Ys):- 
      getRed(Xs,[], Red), getWhite(Xs,[],White), getBlue(Xs,[],Blue), 
      append([], Red, Y1), append(Y1, White, Y2), append(Y2, Blue, Ys).
  
  getRed([],Rs,Rs).
  getRed([X|Rest], Acc, Rs) :- red(X), getRed(Rest, [X,Acc] , Rs).
  getRed([X|Rest], Acc, Rs) :- getRed(Rest, Acc, Rs).
  
  getWhite([],Rs,Rs).    
  getWhite([X|Rest], Acc, Rs) :- white(X), getWhite(Rest, [X,Acc], Rs).
  getWhite([X|Rest], Acc, Rs) :- getWhite(Rest, Acc, Rs).
  
  getBlue([],Rs,Rs).
  getBlue([X|Rest], Acc, Rs) :- blue(X), getBlue(Rest, [X,Acc], Rs).    
  getBlue([X|Rest], Acc, Rs) :- getBlue(Rest, Acc, Rs).
  </code></pre>
  
  <p>My output looks like this :</p>
  
  <pre><code>?- dutch([1,2,3],R).
  R = [1, [], 2, [], 3, []]
  R = [1, [], 2, []]
  R = [1, [], 3, []]
  R = [1, []]
  R = [2, [], 3, []]
  R = [3, []]
  R = []
  </code></pre>
  
  <p>What I want is for it to look like this:</p>
  
  <pre><code>R = [1, 2, 3]
  </code></pre>
  
  <p>I've tried a few ways to force the output to what i want, but haven't been able to get anywhere close. </p>
  
  <p>Edit: Looks like i can solve it by using a brute force solution of permuting all possible sets and evaluating whether the set is in "Dutch Flag" order. Is there a better solution though?</p>