×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Prolog "little" issue

## Prolog "little" issue

(OP)
Hi,

I am learning prolog and I am not able to fullfill the following excercise:

1. I have a list of facts:
f(name0).
m(name1).
-> these are persons, f(x) means female, m(x) means male.
child_of(x,y).
-->x is a child of y.

2. I want to now if x is and descendent of y:
descendent(x,y) :- child_of(x,y).
descendent(x,y) :- descendent(x,z), child_of(z,y).

3. so far everything worked fine, but here is my problem:
I want to have a request descendent(x, y, z) which means that
x is a descendent of y and z is the realtion between them.

so if I am requesting:
?- descendent(person1, person2, X).
X = c(person2).
Which means person1 is a child of person 2.
or
?- descendent(person3, person4, X).
X = c(c(person4)).
Which means person 3 is a child of a child of person4.

How can I code that? Any hints would be a pleasure.

### RE: Prolog "little" issue

The design for descendent/2, is almost good : try descendent(name1, name0) and after the answer true type ";" instead of Enter, you will enter an infinite loop !
After having fixed the bug, try the same design for descendent/3.

### RE: Prolog "little" issue

(OP)
I do not get this...

So what I want as output is:
?- descendent(person3, person4, X).
X = c(c(person4)).

so if person3 is a descendent of person4 I can count all the c()
to know how much generations are between the two persons.

with my code I only know if a person is descendent of
another person and this works quite well.

Is it possible to genereate an output like this?!
I am really not sure because I can not find a tutorial
which deals with such a topic...

### RE: Prolog "little" issue

Well here is my code, works with SWI-Prolog

#### CODE --> Prolog

child_of(name1,name0).
child_of(name2,name1).
child_of(name3,name2).

descendent(X,Y) :-
child_of(X,Y).
descendent(X,Y) :-
child_of(Z,Y),
descendent(X,Z). 
If I use

#### CODE --> Prolog

descendent(X,Y) :-
descendent(X,Z),
child_of(Z,Y). 
at the query descendent(name1, X). I get name0, and after pressing <space> (or ";") I get a loop.

For descendent/3, its exactly the same thing :

#### CODE --> Prolog

descendent(X, Y, c(Y)) :-
child_of(X,Y).

descendent(X, Y, c(T)) :-
child_of(X,Z),
descendent(Z,Y, T). 

#### Quote:

?- descendent(name3, name0, X).
X = c(c(c(name0))) ;
false.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!