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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Prolog Program - Please help!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do i write a program that computes the sum of nested lists of numbers?

The list can contain either just numbers, of lists of numbers.
An example of how the program should work is:
?-sum([1,2,[3,4],5],S) would return S=15.

Right now I can add just numbers, but not lists of numbers:
sum([],0).
sum([H|T],N) :-
sum(T,N1),
N is H + N1.
 
How about this program?

sum([], 0) :- !.

sum(N, N) :- number(N), !.

sum([H|T], N) :-
sum(H, N1),
sum(T, N2),
N is N1 + N2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top