title: How to implement flatten list in prolog ,with tail recursion?
body: <p>How to implement flatten list in prolog ,with tail recursion ?</p>
  
  <p>This is code for flatten/2 with simple recursion (that is mean without back-tracking):</p>
  
  <pre><code>flatten([], []).
  flatten([L|Ls], FlatL) :-
      !,
      flatten(L, NewL),
      flatten(Ls, NewLs),
      append(NewL, NewLs, FlatL).
  flatten(L, [L]).
  
  ?- flatten([1, [2,3], [4]], X).
  X=[1,2,3,4].
  </code></pre>
  
  <p>I'm trying to do the same algorithm but with tail recursion (Accumulator). For exemple, the predicate <code>sum/2</code> returns the addition of all member of the list, with backtracking:</p>
  
  <pre><code>sum([X],[X]).
  sum([H|T],S) :- sum(T,S1), S is H + S1 .
  </code></pre>
  
  <p>the same algo with tail recursion is </p>
  
  <pre><code>sum1(L,S) :- sum1(L,0,S).
  
  sum1([],Acc,Acc).
  sum1([H|T],Acc,S) :- Acc1 is Acc+H, s(T,Acc1,S).
  </code></pre>