Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地

一个供Prolog爱好者学习与交流的地方


您没有登录。 请登录注册

以prolog程式建立一個家庭要找祖父的名稱如何定義如何做~

浏览上一个主题 浏览下一个主题 向下  留言 [第1页/共1页]

family(person(amy,fox,date(5,may,1952),work(baseball,160000)),
person(jed,fox,date(20,may,1960),unemployed),
[person(herman,fox,date(24,may,1979),work(stylist,42000)),
person(uerica,fox,date(13,may,1989),work(clerk,7000))]).

family(person(susan,fox,date(19,may,1947),work(car,15200)),
person(peter,fox,date(18,may,1965),unemployed),
[person(isaac,fox,date(17,may,1991),work(car,40000)),
person(edward,fox,date(16,may,1973),work(sport,690000))]).
husband(X):-family(X,_,_).
wife(X):-family(_,X,_).
child(X):-family(_,_,children),member(X,Children).
exist(person):-
husband(person)
;
wife(person)
;
child
deteofbirth(person(_,_,Date,_),Date).
salary(person(_,_,_,works(_,s)),s).
salary(person(_,_,_,unemployed),0).
male(X):-family(_,_,[X|_]);family(X,_,_).
female(X):-family(_,_,[_|X]);family(_,X,_).
parent(X,Y):-father(X,Y).
parent(X,Y):-mother(X,Y).
grandfather(X,Y):-parent(X,Z),parent(Z,Y).male(X).
grandmother(X,Y):-parent(X,Z),parent(Z,Y).female(X).

grandparent(X,Y):-parent(X,Z),parent(Z,Y).
male(jack).
male(Peter).
male(john).
male(david).
male(karen).
male(jed).
male(bert).
male(andrew).
male(arthur).
male(bertran).
male(hank).
male(cary).
male(dale).
male(harold).
female(susan).
female(amy).
female(ray).
female(mary).
female(liza)
female(Susan).
female(lydia).
female(vicky).
female(aspasia).
female(babs).
female(aileen).
female(jean).
female(ida)
female(harriet).
father(jack,susan).
father(jack,ray).
father(david,liza).
father(david,john).
father(john,peter).
father(john,mary).
father(bert,lucy ).
father(bert,vicky).
father(andrew,lydia).
father(andrew,amy).
mother(karen,susan).
mother(karen,ray).
mother(amy,jed).
mother(amy,john).
mother(susan,peter).
mother(susan,mary).
mother(vicky,karen).
mother(vicky,Jed).
mother(lydia,andrew).
mother(lydia,Peter).



如提有十個家庭要找名字不一樣的祖父的定義要如何寫?只要有雙親就必須找到祖父的對應關係~ 有些不需要的麻煩幫我刪除~ 修正正確的教導我~ 需要增加幫我修正希望可以找出關係~ 謝謝!!

查阅用户资料
至少需这些句子就可以查询了:

代码:
parent(X,Y):- father(X,Y).
parent(X,Y):- mother(X,Y).
grandfather(X,Y):-parent(X,Z),parent(Z,Y), male(X).
grandmother(X,Y):-parent(X,Z),parent(Z,Y), female(X).

male(jack).
male(peter).
male(john).
male(david).
male(jed).
male(bert).
male(andrew).
male(arthur).
male(bertran).
male(hank).
male(cary).
male(dale).
male(harold).
female(susan).
female(amy).
female(ray).
female(mary).
female(liza).
female(susan).
female(lydia).
female(vicky).
female(aspasia).
female(babs).
female(aileen).
female(jean).
female(ida).
female(harriet).
female(karen).
father(jack,susan).
father(jack,ray).
father(david,liza).
father(david,john).
father(john,peter).
father(john,mary).
father(bert,lucy ).
father(bert,vicky).
father(andrew,lydia).  % 句1
father(andrew,amy).
mother(karen,susan).
mother(karen,ray).
mother(amy,jed).
mother(amy,john).
mother(susan,peter).
mother(susan,mary).
mother(vicky,karen).
mother(vicky,jed).
mother(lydia,andrew).  % 句2
mother(lydia,peter).

不过有个奇怪的地方,句1和句2好像是矛盾的。

查询结果:

8 ?- grandfather(X, _).
X = jack ;
X = jack ;
X = david ;
X = david ;
X = bert ;
X = bert ;
X = andrew ;
X = andrew ;
X = andrew ;
X = andrew ;
false.




Jessicaluin 写道::family(person(amy,fox,date(5,may,1952),work(baseball,160000)),
person(jed,fox,date(20,may,1960),unemployed),
[person(herman,fox,date(24,may,1979),work(stylist,42000)),
person(uerica,fox,date(13,may,1989),work(clerk,7000))]).

family(person(susan,fox,date(19,may,1947),work(car,15200)),
person(peter,fox,date(18,may,1965),unemployed),
[person(isaac,fox,date(17,may,1991),work(car,40000)),
person(edward,fox,date(16,may,1973),work(sport,690000))]).
husband(X):-family(X,_,_).
wife(X):-family(_,X,_).
child(X):-family(_,_,children),member(X,Children).
exist(person):-
husband(person)
;
wife(person)
;
child
deteofbirth(person(_,_,Date,_),Date).
salary(person(_,_,_,works(_,s)),s).
salary(person(_,_,_,unemployed),0).
male(X):-family(_,_,[X|_]);family(X,_,_).
female(X):-family(_,_,[_|X]);family(_,X,_).
parent(X,Y):-father(X,Y).
parent(X,Y):-mother(X,Y).
grandfather(X,Y):-parent(X,Z),parent(Z,Y).male(X).
grandmother(X,Y):-parent(X,Z),parent(Z,Y).female(X).

grandparent(X,Y):-parent(X,Z),parent(Z,Y).
male(jack).
male(Peter).
male(john).
male(david).
male(karen).
male(jed).
male(bert).
male(andrew).
male(arthur).
male(bertran).
male(hank).
male(cary).
male(dale).
male(harold).
female(susan).
female(amy).
female(ray).
female(mary).
female(liza)
female(Susan).
female(lydia).
female(vicky).
female(aspasia).
female(babs).
female(aileen).
female(jean).
female(ida)
female(harriet).
father(jack,susan).
father(jack,ray).
father(david,liza).
father(david,john).
father(john,peter).
father(john,mary).
father(bert,lucy ).
father(bert,vicky).
father(andrew,lydia).
father(andrew,amy).
mother(karen,susan).
mother(karen,ray).
mother(amy,jed).
mother(amy,john).
mother(susan,peter).
mother(susan,mary).
mother(vicky,karen).
mother(vicky,Jed).
mother(lydia,andrew).
mother(lydia,Peter).



如提有十個家庭要找名字不一樣的祖父的定義要如何寫?只要有雙親就必須找到祖父的對應關係~ 有些不需要的麻煩幫我刪除~ 修正正確的教導我~ 需要增加幫我修正希望可以找出關係~ 謝謝!!



由Mercury Liao于周四 十一月 22, 2012 2:49 am进行了最后一次编辑,总共编辑了1次

查阅用户资料 http://prolog.longluntan.net
請問句1和句2矛盾是要刪掉的意思嗎?

查阅用户资料
Jessicaluin 写道::請問句1和句2矛盾是要刪掉的意思嗎?

它实际意义上是矛盾的,程序上不矛盾,
不晓得你懂我的意思不?

因为实际意义上,不可能andrew是lydia的父亲,lydia又是andrew的母亲吧!

查阅用户资料 http://prolog.longluntan.net
恩~ 我知道~ 矛盾!! 謝謝你的教導~

查阅用户资料
[quote="Mercury Liao"][quote="Jessicaluin"]請問句1和句2矛盾是要刪掉的意思嗎?[/quote]

它实际意义上是矛盾的,程序上不矛盾,
不晓得你懂我的意思不?

因为实际意义上,不可能andrew是lydia的父亲,lydia又是andrew的母亲吧!
[/quote]



那要每個家庭都尋找祖母~是一樣的語法嗎? 還是有哪裡又不一樣呢?

查阅用户资料
Jessicaluin 写道::
Mercury Liao 写道::
Jessicaluin 写道::請問句1和句2矛盾是要刪掉的意思嗎?

它实际意义上是矛盾的,程序上不矛盾,
不晓得你懂我的意思不?

因为实际意义上,不可能andrew是lydia的父亲,lydia又是andrew的母亲吧!



那要每個家庭都尋找祖母~是一樣的語法嗎? 還是有哪裡又不一樣呢?

查询时把grandfather改成grandmother就可以。
我前面写的代码grandmother写错了写成grandparent,已经改过来了。

查阅用户资料 http://prolog.longluntan.net

浏览上一个主题 浏览下一个主题 返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题