简体   繁体   English

通过Prolog获得独特的结果

[英]Get unique results with Prolog

I have this Prolog code that returns: [[vincent,vincent],[vincent,marcellus],[marcellus,vincent],[marcellus,marcellus],[pumpkin,pumpkin],[honey_bunny,honey_bunny]] . 我有返回的此Prolog代码: [[vincent,vincent],[vincent,marcellus],[marcellus,vincent],[marcellus,marcellus],[pumpkin,pumpkin],[honey_bunny,honey_bunny]]

:- initialization main.

loves(vincent, mia).
loves(marcellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X, Y) :-
    loves(X, Z),
    loves(Y, Z).

main :- 
    findall([X, Y], jealous(X, Y), L),
    write(L),
    halt.

How to get the only results when X != Y? 当X!= Y时如何获得唯一结果? I tried the following code to get the same results as before. 我尝试使用以下代码来获得与以前相同的结果。

jealous(X, Y) :-
    X \== Y,
    loves(X, Z),
    loves(Y, Z).

With \\= , I got [] . 使用\\= ,我得到了[] How to get only [vincent,marcellus] as a result? 结果如何只获得[vincent,marcellus]

The order of the goals in your attempted solution is wrong. 您尝试的解决方案中的目标顺序是错误的。 When called with two distinct variables, the (\\==)/2 standard predicate always succeed. 当使用两个不同的变量调用时, (\\==)/2标准谓词始终会成功。 The solution is to call the predicate only when its arguments are instantiated: 解决方案是仅在实例化其参数时调用谓词:

jealous(X, Y) :-
    loves(X, Z),
    loves(Y, Z),
    X \== Y.

With this fix, your query now returns: 使用此修复程序,您的查询现在返回:

?- findall([X, Y], jealous(X, Y), L).
L = [[vincent, marcellus], [marcellus, vincent]].

So, no one is jealous of himself anymore. 因此,没有人会嫉妒自己。 But you still get a redundant solution. 但是您仍然可以获得冗余解决方案。 We can modify the jealous/2 predicate to sort the names in the returned solutions. 我们可以修改jealous/2谓词以返回的解决方案中的名称进行排序 For example: 例如:

jealous(X, Y) :-
    loves(X0, Z),
    loves(Y0, Z),
    X0 \== Y0,
    (   X0 @< Y0 ->
        X = X0, Y = Y0
    ;   X = Y0, Y = X0
    ).

Now, by using setof/3 instead of findall/3 , we get: 现在,通过使用setof/3而不是findall/3 ,我们得到:

?- setof([X, Y], jealous(X, Y), L).
L = [[marcellus, vincent]].

One final observation. 最后的观察。 A list is a poor solution for representing a pair . 列表对于表示一是不好的解决方案。 The traditional way is to use either XY or (X, Y) . 传统方法是使用XY(X, Y)

Whenever possible, use dif/2 instead of (\\==)/2 . 尽可能使用dif/2代替(\\==)/2

dif/2 will help you write logically sound programs. dif/2将帮助您编写逻辑上合理的程序。

For details, look at ! 有关详细信息,请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM