首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SWI-PROLOG结构

SWI-PROLOG结构
EN

Stack Overflow用户
提问于 2013-06-13 19:46:53
回答 2查看 506关注 0票数 0
代码语言:javascript
复制
    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中显示查找所有家庭的所有兄弟?我将非常感谢您的帮助:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-13 20:53:37

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

代码语言:javascript
复制
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.

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

代码语言:javascript
复制
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.
票数 2
EN

Stack Overflow用户

发布于 2013-06-13 20:58:17

这是可行的..。

代码语言:javascript
复制
brother(Child1, Child2):-
    child(Child1), gender(Child1, male),
    child(Child2),
    Child1 \= Child2,
    lastname(Child1, LastName),
    lastname(Child2, LastName).

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17086270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档