×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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

[NEED HELP] Recursion case

[NEED HELP] Recursion case

[NEED HELP] Recursion case

(OP)
I'm just starting to learn prolog. And i'm getting stuck in this problem. May be anyone can help me out of this.

Given some fact below :

byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).

byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).

byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(losAngeles,auckland).


and i define predicate travel/2 as follow :
travel(A,B):-bycar(A,B);bytrain(A,B);byplane(A,B).
travel(A,B)sadbycar(A,C);bytrain(A,C);byplane(A,C)).



And the problem is :
1. By using travel/2 to query the above database, write a predicate travel/3 which
tells how to travel from one place to another. The program should, e.g., answer
‘yes’ to the query travel(valmont,paris,go(valmont,metz,go(metz,paris)))
and X = go(valmont,metz,go(metz,paris,go(paris,losAngeles))) to the
query travel(valmont,losAngeles,X).
2. Extend the predicate travel/3 so that it not only tells via which other cities
you have to go to get from one place to another, but also how, i.e. by car, train,
or plane, you get from one city to the next.

RE: [NEED HELP] Recursion case

(OP)
I'm just starting to learn prolog. And i'm getting stuck in this problem. May be anyone can help me out of this.

Given some fact below :

CODE --> prolog

byCar(auckland,hamilton).
byCar(hamilton,raglan).
byCar(valmont,saarbruecken).
byCar(valmont,metz).

byTrain(metz,frankfurt).
byTrain(saarbruecken,frankfurt).
byTrain(metz,paris).
byTrain(saarbruecken,paris).

byPlane(frankfurt,bangkok).
byPlane(frankfurt,singapore).
byPlane(paris,losAngeles).
byPlane(bangkok,auckland).
byPlane(losAngeles,auckland). 


and i define predicate travel/2 as follow :

CODE -->

travel(A,B):-bycar(A,B);bytrain(A,B);byplane(A,B).
travel(A,B)bycar(A,C);bytrain(A,C);byplane(A,C)). 


And the problem is :
1. By using travel/2 to query the above database, write a predicate travel/3 which
tells how to travel from one place to another. The program should, e.g., answer
‘yes’ to the query travel(valmont,paris,go(valmont,metz,go(metz,paris)))
and X = go(valmont,metz,go(metz,paris,go(paris,losAngeles))) to the
query travel(valmont,losAngeles,X).
2. Extend the predicate travel/3 so that it not only tells via which other cities
you have to go to get from one place to another, but also how, i.e. by car, train,
or plane, you get from one city to the next.

RE: [NEED HELP] Recursion case

First, notice that your facts are byCar (with uppercase for Car) and bycar (with lowercase) for rules !

For the first question :

CODE

% if there is a junction
travel(From, To, go(From, To)) :-
	travel(From, To).

% else, we try another town connected with From
% and we query a Path from X to To.
% Backtrack will find the Path  if it exist.
travel(From, To, go(From, X, Path)) :-
	travel(From, X),
	travel(X, To, Path). 

For the second question :

CODE

travel_2(From, To, go(With)) :-
	byCar(From, To), With = byCar(From, To);
	byTrain(From, To), With = byTrain(From, To);
	byPlane(From, To), With = byPlane(From, To).

travel_2(From, To, go(With, Path)) :-
	travel_2(From, X, go(With)),
	travel_2(X, To, Path). 

RE: [NEED HELP] Recursion case

Be aware that in this code, there is no search for cycles !

RE: [NEED HELP] Recursion case

(OP)
Thanks for the answer. it's very helpfull.

By the way, there is some typos for travel/2. Here is the correct one :

CODE -->

travel(A,B):-byCar(A,B);byTrain(A,B);byPlane(A,B).
travel(A,B):-(byCar(A,C);byTrain(A,C);byPlane(A,C)),travel(C,B). 

The second problem it was solved, but for the first, when i try this query :

CODE -->

travel(valmont,losAngeles,X). 
The only answer is :

CODE -->

X = go(valmont,LosAngeles). 

note : i'm using your rule for this.

RE: [NEED HELP] Recursion case

(OP)
i'm sorry before. that is not the only answer for the query.
but it is infinite as below :

CODE --> travel(valmont,losAngeles,X).

X = go(d, c) ;
X = go(d, c) ;
X = go(d, c) ;
X = go(d, c) ;
X = go(d, e, go(e, c)) ;
X = go(d, e, go(e, c)) ;
X = go(d, e, go(e, g, go(g, c))) ;
X = go(d, e, go(e, g, go(g, i, go(i, c)))) ;
X = go(d, e, go(e, g, go(g, i, go(i, k, go(k, c))))) ;
X = go(d, e, go(e, g, go(g, i, go(i, k, go(k, a, go(a, c)))))) ;
X = go(d, e, go(e, g, go(g, i, go(i, k, go(k, a, go(a, b, go(b, c))))))) ;
X = go(d, e, go(e, g, go(g, i, go(i, k, go(k, b, go(b, c))))))
.... 


any idea how to simplify the answer?.

RE: [NEED HELP] Recursion case

I didn't use the second rule travel(A,B) :- (byCar(A,C);byTrain(A,C);byPlane(A,C)),travel(C,B). because it hides the town C, it is not needed to solve the problem.
Have you to use it ?

RE: [NEED HELP] Recursion case

(OP)
Yaa.. coz rule travel(A,B) :- (byCar(A,C);byTrain(A,C);byPlane(A,C)),travel(C,B). is needed to solve a question before this. So, this rule should be in my code.

this is the question before :

Write a predicate travel/2 which determines whether it is possible to travel
from one place to another by ‘chaining together’ car, train, and plane journeys.
For example, the program should answer ‘yes’ to the query

travel(valmont,raglan).

RE: [NEED HELP] Recursion case

OK but have you to use former predicates in new code ?

RE: [NEED HELP] Recursion case

(OP)
Thanks mr. Joel. It was solved.

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