开发者

Prolog: X is the grandfather of Y

开发者 https://www.devze.com 2023-01-29 03:43 出处:网络
Assume that the following facts are already entered into the Prolog database: father(X, Y)// X is the father of Y

Assume that the following facts are already entered into the Prolog database:

father(X, Y)     // X is the father of Y
mother(X, Y)     // X is the mother of Y
male(X)          // X is a male
female(X)        // X is a female
parent(X, Y)     // X is a parent of Y
diff(X, Y)       // X and Y are different

(1) Now add a Prolog rule for grandpa_of(X, Y) where "X is the grandfather of Y"

(2) Add another rule for sibling(X, Y) where "X is the sibling of Y"

M开发者_开发技巧y thoughts:

Question 1:

I am confused on how I can find the parents of the parents, all I have so far is

grandpa_of(X,Y) :- male(X), ...

Question 2:

sibling(X, Y) :- parent(P, X), parent(P, Y), diff(X, Y)


I think Jason means grandpa_of(X,Y) :- father(X,P), parent(P,Y).


It has been a long time... The first one is something like this:

grandpa_of(X, Y) :- father(X, P), father(P, Y).

Been too long... :-P


Hamad is the parent of Bilal, Laiba, and Marium. Ali and Mona are Hamad's parents. Musarat is also the parent of Bilal, Laiba, and Marium. Kamran and Javeria are her parents. Kamran and Javeria are also the parents of Salma, and Umar. Parents are ancestors as are ancestors of parents. Siblings share a parent.

  1. mother
  2. brother
  3. grandfather
  4. uncle
  5. aunt
male(hamad).
male(bilal).
male(ali).
male(kamran).
male(umar).

female(laiba).
female(marium).
female(mona).
female(musarat).
female(javeria).
female(salma).

parents(ali, hamad).
parents(mona, hamad).

parents(kamran, musarat).
parents(javeria, musarat).

parents(kamran, salma).
parents(kamran, umar).

parents(javeria, salma).
parents(javeria, umar).

parents(hamad, bilal).
parents(hamad, laiba).
parents(hamad, marium).

parents(musarat, bilal).
parents(musarat, laiba).
parents(musarat, marium).



mother(X, Y) :-
    parents(X, Y),
    female(X),
    write(X),
    write(' is a mother of '),
    write(Y),
    nl.

brother(X, Y) :-
    male(X),
    x \= y,
    parents(Z, X),
    parents(Z, Y),
    write(X),
    write(' is a brother of '),
    write(Y),
    nl.

sister(X, Y) :-
    female(X),
    x \= y,
    parents(Z, X),
    parents(Z, Y),
    write(X),
    write(' is a sister of '),
    write(Y),
    nl.

grandfather(X, Y) :-
    parents(X, P),
    parents(P, Y),
    male(X),
    write(X),
    write(' is grandfather of '),
    write(Y),
    nl.

uncle(X, Z) :-
    brother(X, P),
    parents(P, Z),
    male(X),
    write(X),
    write(' is an uncle of '),
    write(Z),
    nl.

aunt(X, Z) :-
    sister(X, P),
    parents(P, Z),
    female(X),
    write(X),
    write(' is an aunt of '),
    write(Z),
    nl.


%Sample program of X is a Grandfather of Y

%knowledgebase or database

female(amrita).

female(aroti).

female(manusi).

female(parboti).

male(susanta).

male(prasanta).

male(haran).

male(ratan).

male(srijit).

%clause

parent(manusi,susanta).

parent(ratan,amrita).

parent(aroti,amrita).

parent(haran,prasanta).

parent(haran,susanta).

parent(manusi,prasanta).

parent(susanta,srijit).

%rules

mother(X,Y):-parent(X,Y),female(X).

father(X,Y):-parent(X,Y),male(X).

sister(X,Y):-female(X),x==y,parent(Z,X),parent(Z,Y).

brother(X,Y):-male(X),x==y,parent(Z,X),parent(Z,Y).

grandparent(X,Y):-parent(X,Y),parent(Y,Z).

grandmother(X,Y):-mother(X,Y),parent(Y,Z).

grandfather(X,Y):-father(X,Y),parent(Y,Z).

0

精彩评论

暂无评论...
验证码 换一张
取 消