title: SWI Prolog: difference lists - fifo
body: <p>I am learning Prolog and difference lists are super new to me and looks like from hell, especially that I am definitely poor simple-minded C++ guy :)
  I have problem with my task:</p>
  
  <blockquote>
    <p>addall(-E, +G, +S, -R) adds all results of
    substitutions for the variable E which satisfy
    the goal G (in which the variable E occurs) to
    a collection S and returns a new collection R
    (this predicate resembles the standard predicates findall=3 and findall=4).</p>
  </blockquote>
  
  <p>I have implemented this with LIFO collection with no problem but something is wrong with FIFO, consider following usage:</p>
  
  <pre><code>lifo_empty(L), lifo_addall(X,number(odd,X), L, W).
  </code></pre>
  
  <p>is ok, collection W is [8,6,4,2,0] but:</p>
  
  <pre><code>fifo_empty(Q), fifo_addall(X, podstaw(c,X), Q, W).
  </code></pre>
  
  <p>gives me <code>W = [9, 7, 5, 3|[1|_G3761]-_G3761]</code>. What I want is <code>[9,7,5,3,1|_G3761]-_G3761</code>. I have no idea what is wrong, especially that:</p>
  
  <pre><code>?- fifo_empty(Q), fifo_put(a,Q,W), fifo_put(b,W,C).
  C = [a, b|_G3922]-_G3922.
  </code></pre>
  
  <p>works just fine. My code:</p>
  
  <pre><code>fifo_empty(X-X).
  todiff(X, [X|Xs]-Xs).
  fifo_put(E, X-[E|Xs], X-Xs).
  fifo_get([E|X]-Xs, X-Xs, E).
  
  fifo_append([],S,S).
  fifo_append([H|T],S,W) :- fifo_put(H,S,NS), lifo_append(T,NS,W).
  
  fifo_addall(E, Goal, S, R) :- findall(E,Goal, W), fifo_append(W,S,R).
  </code></pre>
  
  <p>Thanks in advance.</p>