Skip to content

Chapter 3 exercise Exercise 3.3 #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kagankorkmaz opened this issue May 29, 2020 · 1 comment
Open

Chapter 3 exercise Exercise 3.3 #51

kagankorkmaz opened this issue May 29, 2020 · 1 comment

Comments

@kagankorkmaz
Copy link

Are you sure about the solution? It looks like OUT OF LOCAL STACK will be occur.

@Victordmz
Copy link

Victordmz commented Mar 8, 2021

This one works. The ! is a cut, meaning once a solution is found, Prolog does not have to backtrack (and thus does not have to find any alternative routes). The trick was to use another functor for the other direction, called travelBetweenReverseDirection, so that Prolog cannot go back to the initial direction using travelBetween.

directTrain(forbach,saarbruecken).
directTrain(freyming,forbach).
directTrain(fahlquemont,stAvold).
directTrain(stAvold,forbach).
directTrain(saarbruecken,dudweiler).
directTrain(metz,fahlquemont).
directTrain(nancy,metz).

travelBetween(X,Y) :- directTrain(X,Y),!.
travelBetween(X,Y) :-
	directTrain(X,Z),
	travelBetween(Z,Y),
	!.
travelBetween(X,Y) :- travelBetweenReverseDirection(Y,X),!.
travelBetweenReverseDirection(X,Y) :- directTrain(X,Y).
travelBetweenReverseDirection(X,Y) :-
	directTrain(X,Z),
	travelBetweenReverseDirection(Z,Y).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants