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.如何检查brother/2 Child1和Child2是否来自同一家庭?我不认为我的是正确的。如何在SWI-Prolog中显示查找所有家庭的所有兄弟?我将非常感谢您的帮助:)
发布于 2013-06-13 20:53:37
我假设Child1和Child2是person结构?如果是这样,那么应该使用您当前的结构:
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.发布于 2013-06-13 20:58:17
这是可行的..。
brother(Child1, Child2):-
child(Child1), gender(Child1, male),
child(Child2),
Child1 \= Child2,
lastname(Child1, LastName),
lastname(Child2, LastName).但是你使用的“面向对象”的方法不能很好地扩展,因为Prolog是基于关系数据模型的。
https://stackoverflow.com/questions/17086270
复制相似问题