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

Prolog problem - Tuples

Status
Not open for further replies.

dadin22

Programmer
Apr 5, 2010
1
US
tuples(?List1,?List2,?List3,?Pad,?Tuples)

succeeds if Tuples is a list of tuples composed from corresponding elements in List1, List2, and List3. A tuple is either pair(X1,X2), or triple(X1,X2,X3), and Pad is used for padding if the lists are too short to create pairs.

Ex: tuples([a,b],[1,2,3],[z],x, [triple(a,1,z),pair(b,2),pair(3,x)])

Any clue how should I approach this ?

I am not asking for an answer to the problem but only hint for direction
 
write something like this:

tuples([H1|T1], [H2|T2], [H3|T3], Pad, [X|T]) :-
tuples(T1, T2, T3, Pad, T),
X = triple(H1, H2, H3).

then put particular cases before this rule ... particular cases are those where one list gets empty before the others ...

for example:

tuples([H1|T1], [], [H3|T3], Pad, [X|T]) :-
tuples(T1, [], T3, Pad, T),
X = pair(H1, H3).

... and so on ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top