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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[NEED HELP] Recursion case 1

Status
Not open for further replies.

sirJS

Programmer
Nov 16, 2012
7
ID
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 :

[highlight #A40000]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).
[/highlight]

and i define predicate travel/2 as follow :
[highlight #A40000]travel(A,B):-bycar(A,B);bytrain(A,B);byplane(A,B).
travel(A,B):-(bycar(A,C);bytrain(A,C);byplane(A,C)).[/highlight]


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.

 
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:
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.
 
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).
 
Be aware that in this code, there is no search for cycles !
 
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.
 
i'm sorry before. that is not the only answer for the query.
but it is infinite as below :

Code:
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?.
 
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 ?
 
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).

 
OK but have you to use former predicates in new code ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top