开发者

Duplicate solutions

开发者 https://www.devze.com 2023-01-19 00:48 出处:网络
I have a problem trying to get some code that returns unique answers to my query. For example, defining

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.


You can also use

| ?- setof(sol(A,B),morestuff([A,A],[A,B],[a,b]),L).
L = [sol(a,b)] ? 
yes


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号