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:
static void add(double[] array){
double x = .0;
for (int i = 0; i < array.length; ++i)
x += array[i];
System.out.println(x);
}
However, I have been scratching my head about how to do it in prolog. I have the following 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).
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.
Any help will be greatly appreciated.
Thanks.