title: Flattening a list
body: <p>Trying to solve exercise 07 from <a href="http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/" rel="nofollow">http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/</a> </p>
  
  <p>I've started with a single iteration which looks like following</p>
  
  <pre><code>my_flatten1([], []).
  my_flatten1([[A|T]|U], [A|V]) :-
     append(T, U1, V),
     my_flatten1(U, U1).
  my_flatten1([A|T], [A|U]) :-
     not(is_list(A)),
     my_flatten1(T, U).
  
  is_flat(A) :-
     my_flatten1(A, A).
  </code></pre>
  
  <p>it seems to work just fine for the following set of queries</p>
  
  <pre><code>my_flatten1([a, [b, [c, d], e]], X).
  my_flatten1(X, [a, b, c]).
  my_flatten1(X, [a|T]).
  my_flatten1(X, [a, b, A]).
  my_flatten1([a, X], M).
  my_flatten1([a|X], M).
  
  is_flat([a|X]).
  is_flat([a,[c]|X]).
  is_flat([F,[c]|X]).
  
  my_flatten1(A, B).
  my_flatten1([A], B).
  my_flatten1([[A]], B).
  my_flatten1([[a|A]], B).
  my_flatten1([a|A], B).
  my_flatten1([X|B], [1,2,3,4]).
  my_flatten1([[a,c|D]|X], [a|B]).
  </code></pre>
  
  <p>However I wasn't successful to build <code>my_flatten</code> based on <code>my_flatten1</code>. There's always some queries that fail or end up in endless loops, or produce just one, most obvious result.</p>
  
  <p>Edit to clarify what I am after, for example I can call <code>my_flatten1</code> with a variable as a first argument in order to distribute brackets:</p>
  
  <pre><code>?- my_flatten1(X, [a, b]).
  X = [[a], [b]] ;
  X = [[a], b] ;
  X = [[a, b]] ;
  X = [a, [b]] ;
  X = [a, b].
  </code></pre>