title: Flatten a list in Prolog
body: <p>I've only been working with Prolog for a couple days.  I understand some things but this is really confusing me.</p>
  
  <p>I'm suppose to write a function that takes a list and flattens it.</p>
  
  <pre><code>?- flatten([a,[b,c],[[d],[],[e]]],Xs).  
  Xs = [a,b,c,d,e].                           % expected result
  </code></pre>
  
  <p>The function takes out the inner structures of the list.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>flatten2([],[]).
  flatten2([Atom|ListTail],[Atom|RetList]) :-
        atom(Atom), flatten2(ListTail,RetList).
  flatten2([List|ListTail],RetList) :-
        flatten2(List,RetList).
  </code></pre>
  
  <p>Now, this works when I call:</p>
  
  <pre><code>?- flatten2([a,[b,c],[[d],[],[e]]], R).
  R = [a,b,c,d,e].                         % works as expected!
  </code></pre>
  
  <p>But when I call to see if a list that I input is already flattened, is returns <code>false</code> instead of <code>true</code>:</p>
  
  <pre><code>?- flatten2([a,[b,c],[[d],[],[e]]], [a,b,c,d,e]).
  false.                                   % BAD result!
  </code></pre>
  
  <p>Why does it work on one hand, but not the other?  I feel like I'm missing something very simple.</p>