Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate solutions

I have a problem trying to get some code that returns unique answers to my query. For example, defining

stuff(A,B,C) :- A=C ; B=C.
morestuff([],[],[]).
morestuff([A|AA],[B|BB],[C|CC]) :- stuff(A,B,C), morestuff(AA,BB,CC).

then running

morestuff([A,A],[A,B],[a,b]).

gives the output:

A = a
B = b ? ;

A = a
B = b ? ;

yes.

As you can see the two solutions are the same. Is there a way of just getting PROLOG to return the unique solutions, i,e. give the output:

A = a
B = b ? ;

yes.
like image 334
Lucas Avatar asked Oct 03 '10 15:10

Lucas


2 Answers

You can also use

| ?- setof(sol(A,B),morestuff([A,A],[A,B],[a,b]),L).
L = [sol(a,b)] ? 
yes
like image 155
Michael Leuschel Avatar answered Sep 20 '22 21:09

Michael Leuschel


The only way that I know is to use findall/3 to generate all results, then remove duplicates yourself. (Barring the most obvious solution - avoid algorithms that overgenerate; but then, in many cases you can't do that.)

like image 34
Amadan Avatar answered Sep 19 '22 21:09

Amadan