title: How to determine the sum of 2 numbers written in list representation prolog
body: <p>I'm trying to print the sum of 2 numbers written in list representation.For example for</p>
  
  <pre><code>suma ([5,7,3],[3,1,4],L)
  </code></pre>
  
  <p>the result should be:</p>
  
  <pre><code>L=[8,8,7].
  </code></pre>
  
  <p>This is the program I have written so far:</p>
  
  <pre><code> add_final(E,[],[E]).
   add_final(E,[H|T],[H|T1]):-
   add_final(E,T,T1).
   invers([],[]).
   invers([H|T],L):-
   invers(T,L1),add_final(H,L1,L).
  
   sum([],[],T,[T]):-
                 T=\=0,!.
   sum([],[],_,[]):-!.
  
   sum([],[L],T,[L]):-T=0,!.
   sum([L],[],T,[L]):-T=0,!.
  
   sum([H1|T1],[H2|T2],T,[Hs|Ts]):-
        S=H1+H2+T,
        Tn=S div 10,
        Hs=S mod 10,
        sum(T1,T2,Tn,Ts).
  
   suma(L1,L2,Ls):-
      invers(L1,L1i),
      invers(L2,L2i),
      sum(L1i,L2i,0,Ls).
  </code></pre>
  
  <p>The output of my program is:</p>
  
  <pre><code> L = [ (3+4+0)mod 10, (7+1+ (3+4+0)div 10)mod 10, (5+3+ (7+1+ (... + ... + 0)div 10)div 10)mod 10].
  </code></pre>
  
  <p>What is going on and how to fix it?</p>