Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Absolute value

Status
Not open for further replies.

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?

 
ERROR: toplevel: Undefined procedure: sum/2 (DWIM could not correct goal)
 
With SWI-Prolog (and other dialects I think), you must type your code in a file and compile it to use the predicate sum at toplevel.
 
I don't know what I did, but it works now. Thanks! All I did was re-save the program I did using notepad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top