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

Please help with Prolog examples

Status
Not open for further replies.

mrtommy

Technical User
May 29, 2011
5
CZ
Hello... I really need help with these examples... Thank You...

1) sum (List, X)

Predicate stored in the variable X total value of all elements of list List.


2) double (List, ListList)

Predicate stored in the variable ListList list where each list element List occurs twice in a row.
 
I have this for first example and I don´t know if is it right...


sum([],0).
sum([H | T], S) :-
integer(H),
sum(T, S1),
S is S1 + H.

 
Fine... That´s great... Thank You... But I don´t know, how to do second example... I really need help... I have a exam on friday...
 
Is that correct?

?- dupli([a,b,c],2,X).
X = [a,a,b,b,c,c,]
 
Or this is correct? Pleas reply...


double([],[]).
double([H|T],[H,H|Y]) :- double(T,Y).
 
Code:
double([],[]).
double([H|T],[H,H|Y]) :- double(T,Y).
This is correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top