g0blu322
Programmer
- Oct 28, 2012
- 4
So this is my 2nd week learning prolog.
I wrote a function to calculate the sum of a list of numbers.
sum([], 0).
sum([Head |Tail], TotalSum) :-
sum(Tail, Sum1),
TotalSum is Head + Sum1.
So:
input: ?- (sum([4,3,-5],X))
output: X=2
However, the goal is get the absolute value of the sum of the list.
So the correct output should be: X=12
I've tried different things with the "abs()" like abs(sum([4,3,-5],X)) or modify the program with putting in some "abs" but didn't work
Where should I use it?
I wrote a function to calculate the sum of a list of numbers.
sum([], 0).
sum([Head |Tail], TotalSum) :-
sum(Tail, Sum1),
TotalSum is Head + Sum1.
So:
input: ?- (sum([4,3,-5],X))
output: X=2
However, the goal is get the absolute value of the sum of the list.
So the correct output should be: X=12
I've tried different things with the "abs()" like abs(sum([4,3,-5],X)) or modify the program with putting in some "abs" but didn't work
Where should I use it?