SWI-PROLOG结构

时间:2013-06-13 11:46:53

标签: prolog

    family(
person(jim, tan, male, hobby([fishing, badminton, swim]), dob(2, 1, 1962)),
person(ann, tan, female, hobby([cooking, badminton]), dob(4,9, 1968)),
[person(ham, tan, male, hobby([fishing, football, swim]), dob(18,5, 1984)),
 person(wan, tan, female, hobby([reading, music, swim]), dob(25, 12, 1986))]
). % Tan's Family(Husband, Wife, [Life_of_Children]).

family(
   person(john, lim, male, hobby([music, reading, watchingTV]), dob(28, 10, 1972)),
   person(belle, lim, female, hobby([music, reading, badminton]), dob(9, 4, 1974)),
   [person(sophia, lim, female, hobby([reading, traveling]), dob(8, 8, 1985)),
person(annie, lim, female, hobby([badminton, volleyball, games]), dob(9, 6, 1987)),
person(william, lim, male, hobby([badminton, swim, games]), dob(10, 7, 1988))]
  ).

% Lim's Family

husband(X):-    %X is a husband if
    family(X, _, _).    %X is the 1st member in family
wife(X):-   %X is a wife if
    family(_, X, _).    %X is a 2nd member in family
child(X):-  %X is a child if
    family(_, _, ChildList),    %X is a member in ChildList
    member(X, ChildList).

family_member(X):-  % X is a family member if
    husband(X); % X is a husband, or
    wife(X);    % X is a wife, or
    child(X).   % X is a child.
gender(Person, X):- % The gender of Person is X if
    Person = person(_, _, X, _,_).  % X matched with the 3rd element in person
interest(Person, X):-   % Interest of Person is X if
    Person = person(_, _, _, hobby(HobbyList),_),
    member(X, HobbyList).   % X is a member in HobbyList.
firstname(X, FirstName):-
    X = person(FirstName, _, _, _, _).
lastname(X, LastName):-
    X = person(_, LastName, _, _, _).
birthday(X, dob(Day, Month, Year)):-
    X = person(_, _, _, _, dob(Day, Month, Year)).
yob(X, Year):-
    X = person(_, _, _, _, dob(_, _, Year)).

father(Father, Child).

brother(Child1, Child2):-
    child(_),
    lastname(Child1, LastName1),
    lastname(Child2, LastName2),
    LastName1 = LastName2.

我如何检查兄弟/ 2 Child1和Child2来自同一个家庭?我不认为我的那个是正确的。以及如何在SWI-Prolog中展示所有家庭中的所有兄弟?我将非常感谢帮助:)

2 个答案:

答案 0 :(得分:2)

我认为Child1Child2person结构?如果是这样,那么这应该使用您当前的结构:

brother(Child1, Child2) :-
    Child1 = person(_, _, male, _, _),  % verify that they're male (brother)
    Child2 = person(_, _, male, _, _),
    family(_, _, Siblings),             % Check to see if they are siblings in the same family
    member(Child1, Siblings),
    member(Child2, Siblings),
    Child1 \= Child2.

这将验证两兄弟。如果这意味着其中只有一个是兄弟那么:

brother(Child1, Child2) :-
    (  % verify that one is male (brother)
       Child1 = person(_, _, male, _, _)
     ; Child2 = person(_, _, male, _, _)
    ),
    family(_, _, Siblings),              % Check to see if they are siblings in the same family
    member(Child1, Siblings),
    member(Child2, Siblings),
    Child1 \= Child2.

答案 1 :(得分:0)

这可行...

brother(Child1, Child2):-
    child(Child1), gender(Child1, male),
    child(Child2),
    Child1 \= Child2,
    lastname(Child1, LastName),
    lastname(Child2, LastName).

但是你正在使用的'面向对象'方法不能很好地扩展,因为Prolog基于关系数据模型。