title: Postfix expression list evaluation
body: <p>I have written a program to evaluate a post-fix expression in prolog recursively from an expression list. For example, given the following list:</p>
  
  <pre><code>[+,1,2]
  </code></pre>
  
  <p>It should return 3. They way I have constructed my predicate is to call itself recursively until it reaches the end of the list so that it reads values backwards. (the same as reading this list from left to right:[2,1,+]).</p>
  
  <p>My problem is that when I try to return more than one value through the recursive calls all the values suddenly disappear.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>eval_list([Head|Tail],_,Result):-
     Tail==[], % last element of list
     Result=Head,
     write(Head),
     write(' was stored in Result!\n').
  
  eval_list([Head|Tail],Store1,Result):-
        eval_list(Tail,Store2, NewResult),
        (\+integer(Store2))
     -&gt;
        % if no integer is bound to Store2, bind Store1 to Head
        Store1=Head,
        Result is NewResult,
        write(Head),
        write(' is stored value!\n')
     ;  (integer(Store2)) -&gt;
      % if an integer is bound to store2, we perform operation specified by the Head with the stored number
        X is Store2+NewResult,
        Result is X,
        write('performed operation!\n')
     ;
        % if doesnt catch either of these states the program is broken
        (  print('something broke\n'),
           print(Store1),
           nl,
           print(Store2),
           nl,
           print(Head),
           nl,
           print(Result),
           nl
        ).
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>?- eval_list([+,1,2],X,Result).
  2 was stored in Result!
  1 is stored value!
  something broke
  _G1162
  _L147
  +
  _G1163
  true.
  </code></pre>
  
  <p>I don't understand why my values disappear, or if there is a better way to evaluate the list.</p>