简体   繁体   中英

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

:- 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? 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?

The order of the goals in your attempted solution is wrong. When called with two distinct variables, the (\\==)/2 standard predicate always succeed. 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. 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([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) .

Whenever possible, use dif/2 instead of (\\==)/2 .

dif/2 will help you write logically sound programs.

For details, look at !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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