Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Students Click Here

Prolog "little" issue

Prolog "little" issue

Prolog "little" issue


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

1. I have a list of facts:
-> these are persons, f(x) means female, m(x) means male.
-->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.
?- 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

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


descendent(X,Y) :-
descendent(X,Y) :-
If I use

CODE --> Prolog

descendent(X,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)) :-

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


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

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close