0

I am trying to find all the brothers of a person.. I have created the following rule..

    find_all_brothers(Z):- findall(X,brother(X,Z),X0),write(X0).

This works however, if a person has more then one brother then it will only find a single brother.. I'm assuming I have to use recursion somehow but I'm a little stuck!

4

1 回答 1

1

如果您有以下关系:

brother(sam, bill).
brother(bill, fred).

而你想找到比尔的所有兄弟,你需要做更多的事情:

find_all_brothers(Z) :-
    findall(X, (brother(X, Z) ; brother(Z, X)), X0), write(X0).

为避免列表中的任何冗余成员,setof将排序并仅提供唯一成员:

find_all_brothers(Z) :-
    setof(X, (brother(X, Z) ; brother(Z, X)), X0), write(X0).
于 2014-02-22T17:22:31.330 回答