title: Issue with Prolog code doing summation of elements in a list
body: <p>Hoping you are well. I am fairly new to prolog and I am having an issue with a code that I am writing. The purpose of the code is quite simple. It adds each element from a list until the last one. Something I could do in Java as:</p>
  
  <pre class="lang-java prettyprint-override"><code>    static void add(double[] array){
          double x = .0;
          for (int i = 0; i &lt; array.length; ++i)
              x += array[i];
          System.out.println(x);
      }
  </code></pre>
  
  <p>However, I have been scratching my head about how to do it in prolog.
  I have the following code</p>
  
  <pre><code>add_list([], X):- write(X).
  add_list([Head|Tail],X) :- 
      Y is Head,
      X is 0 + Y, %initialize X and add Y every time it runs.
      add_list(Tail, X).
  </code></pre>
  
  <p>The error I do get is that the variable X is already bounded when the code runs for the second time which makes sense, but I don't really know how to go about solving the issue.</p>
  
  <p>Any help will be greatly appreciated.</p>
  
  <p>Thanks.</p>