Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write these predicates

Status
Not open for further replies.

lauraberishaj

Programmer
Feb 16, 2007
4
0
0
SE
Hi everybody

I have a problem!

I have a file of prolog which I need to extend with some code.

----------------------------------
woman(anna).
woman(mia).
woman(eva).
woman(maja).
woman(nora).
woman(kim).
woman(elfrida).
woman(ana).
man(john).
man(peter).
man(erik).
man(ben).
man(ed).
man(bill).
man(bull).
man(bert).
man(bob).
man(ari).
man(magnus).

happy(anna).
happy(john).
tall(erik).

loves(john,mia).
loves(peter,mia).
loves(erik,eva).
loves(erik,erik).
loves(eva,erik).

married(erik,eva).
married(mia,peter).
married(bill,maja).

childTo(erik, mia, peter).
childTo(eva, ed, elfrida).
childTo(bill, mia, peter).
childTo(maja, bob, ana).
childTo(ben, bob, ana).
childTo(kim, mia, bob).
childTo(nora, mia, peter).

childTo(anna, erik, eva).
childTo(john, eva, erik).
childTo(bull, bill, maja).
childTo(bert, bill, maja).
childTo(ari, ben, kim).
childTo(magnus, ben, nora).

jealous(X,Y) :-
loves(X,Z),loves(Y,Z),
X\==Y,
\+ happy(X)
.

playsAirGuitar(jody).
playsAirGuitar(X) :-
listensToMusic(X),
happy(X)
.

listensToMusic(mia).
listensToMusic(X) :-
(
woman(X) ;
tall(X), man(X)
)
.

---------------------------

Now I need to add for example :
1. person(P): P is an existing person in the database
2.fatherTo(A,B): A is the father to B
3. father(A): A is a father.
4. siblings(A,B): A and B are siblings (both parents in common)
5. halfSiblings(A,B): A and B are half siblings (only one parent in common)
6. cousins(A,B): A and B are cousins
7. grandmotherTo(A,B): A is a grandmother to B
8. noChildren(A): A has no children
9. ancestor(F,CS,N): F is the ancestor of the people in the list CS and N is the length of CS.


I hope someone can help me, and I would appriciate your help.
Thank You,
Laura

 
1.
Code:
  person(P):-man(P).
  person(P):-woman(P).

2.dont know how you use childTo.assuming child is first then parents.so:
Code:
  fatherTo(A,B):-childTo(B,A,_),man(A).
  fatherTo(A,B):-childTo(B,_,A),man(A).

3.
Code:
  father(A):-man(A),childTo(_,A,_).
  father(A):-man(A),childTo(_,_,A).

and it goes on.
just play with your data,nothing more!

ps.all answers are theoretical and have not been tested.
i just wrote what came in mind first [what i would start from if it was my prob]
 
Hi,

Thanx a lot,
I almost did most of them.
Can you help me with these:

4. siblings(A,B): A and B are siblings (both parents in common)
9. ancestor(F,CS,N): F is the ancestor of the people in the list CS and N is the length of CS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top