Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Prolog program with the following knowledge base: directTrain(union-stat

ID: 3798207 • Letter: W

Question

Write a Prolog program with the following knowledge base:

directTrain(union-station, san-bernardino).
directTrain(oxnard, union-station).
directTrain(burbank, lancaster).
directTrain(lancaster, union-station).
directTrain(san-bernardino, riverside).
directTrain(santa-ana, burbank).
directTrain(oceanside, santa-ana).

This knowledge base holds facts about towns you can travel between by taking a direct train, e.g., from union station to san bernardino.

Write a Prolog recursive predicate travelBetween that tells us when we can travel by train between two towns (not necessarily by a single direct train).

Explanation / Answer

%% We have the following knowledge base: directTrain(nancy,metz). directTrain(metz,fahlquemont). directTrain(fahlquemont,stAvold). directTrain(stAvold,forbach). directTrain(forbach,saarbruecken). directTrain(saarbruecken,dudweiler). directTrain(freyming,forbach). %% That is, this knowledge base holds facts about towns it is possible to travel %% between by taking a direct train. But of course, we can travel further by %% `chaining together' direct train journeys. Write a recursive predicate %% travelBetween/2 that tells us when we can travel by train between two %% towns. For example, when given the query %% travelBetween(nancy,saarbruecken). %% it should reply `yes'. travelBetween(X,Y) :- directTrain(X,Y). travelBetween(X,Y) :- directTrain(X,Z), travelBetween(Z,Y). %% It is, furthermore, plausible to assume that whenever it is possible to take %% a direct train from A to B, it is also possible to take a direct train from B %% to A. Can you encode this in Prolog? You program should e.g. answer `yes' to %% the following query: %% travelBetween(saarbruecken,nancy). %% Do you see any problems you program may run into? travelBetween(X,Y) :- directTrain(X,Y). travelBetween(X,Y) :- directTrain(Y,X). travelBetween(X,Y) :- directTrain(X,Z), travelBetween(Z,Y). travelBetween(X,Y) :- directTrain(Z,X), travelBetween(Z, Y). %% You will end up in infinite loops since you can go both directions, so it is %% possible to keep calling the same function over and over

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote