Removing duplicate solutions

Posted by Enoon on Stack Overflow See other posts from Stack Overflow or by Enoon
Published on 2012-11-15T08:33:32Z Indexed on 2012/11/15 11:01 UTC
Read the original article Hit count: 151

Filed under:
|
|

My code merges two lists of lists, item by item, in the following way:

mergeL([[a,b],[c,d]], [[1,2],[3,4]], Result). Result = [[a,b,1,2],[c,d,3,4]]

And this is the code i use:

mergeL([],[],[]).
mergeL(List, [], List).
mergeL([], List, List).
mergeL([X|Rest],[Y|Rest2], [XY|Res2]) :- mergeL(Rest, Rest2, Res2), append(X,Y,XY).

This seems to work but if i call it with two lists of the same size i get three repeated results. Example (both list contain only one element):

?- mergeL([[a,b]],[[1,2,3]],Q).
Q = [[a, b, 1, 2, 3]] ;
Q = [[a, b, 1, 2, 3]] ;
Q = [[a, b, 1, 2, 3]].

Is there a clean way to make this output only one solution?

© Stack Overflow or respective owner

Related posts about list

Related posts about prolog