我有这个Prolog问题,我想检查3件事。
目前我设法在第(1)部分下面得到这个程序,但是当我运行时,程序出现'是',即使它是错误的路线。不知道为什么。
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).
(1)
connect(X,Y):-byCar(X,Y);byCar(Y,X).
connect(X,Y):-byTrain(X,Y);byTrain(Y,X).
connect(X,Y):-byPlane(X,Y);byPlane(Y,X).
travel(X,Y):-travelLoop(X,Y,[]).
travelLoop(X,Y,_):-connect(X,Y).
travelLoop(X,Y,Passed):-connect(X,Thru),
\+memberchk(Thru,Passed),
travelLoop(Thru,Y,[Thru|Passed]),X\=Y.
travel(_,_):-write('Wrong travel input, please try again.'),nl.
(2)
travel(X,Y,go(X,Y)):-byCar(X,Y).
travel(X,Y,go(X,Y)):-byTrain(X,Y).
travel(X,Y,go(X,Y)):-byPlane(X,Y).
travel(X,Y,go(X,Z,W)):-travel(X,Z,go(X,Z)),travel(Z,Y,W).
(3)
travel1(X,Y,go(X,Y,car)):-byCar(X,Y).
travel1(X,Y,go(X,Y,train)):-byTrain(X,Y).
travel1(X,Y,go(X,Y,plane)):-byPlane(X,Y).
travel1(X,Y,go(X,Z,V,W)):-travel1(X,Z,go(X,Z,V)),travel1(Z,Y,W).
P.S。 (2)和(3)无法想到来回的谓词,只能设法得到一种方式。请帮忙。非常感谢。
答案 0 :(得分:1)
首先,您的connect
谓词会忘记传输模式。将其更改为:
connect(X,Y,byCar):- byCar(X,Y) ; byCar(Y,X).
connect(X,Y,byTrain):- byTrain(X,Y) ; byTrain(Y,X).
connect(X,Y,byPlane):- byPlane(X,Y) ; byPlane(Y,X).
其次,使用travelLoop
路径参数时存在不一致性:
travel(X,Y):-travelLoop(X,Y,[]). %// -- start empty. Why not [X]?
travelLoop(X,Y,_):-connect(X,Y). %// (1)
travelLoop(X,Y,Passed):-connect(X,Thru),
\+memberchk(Thru,Passed),
travelLoop(Thru,Y,[Thru|Passed]), %// -- start from [Thru|...].
X\=Y. %// why here? should be at (1)
travel
的第二个条款只有在第一个不成功时才需要触发。为什么它脱离了?这两个条款应该是文件中的另一个。
travel(_,_):-write('Wrong travel input, please try again.'),nl.
%// This says: succeed always, and tell the user it didn't.
%// Need to join both clauses into one IF: (success -> true; wrong).
travelLoop
的成功条款缺失:
travelLoop(X,Y, Path):- X=Y, %// report the Path to user: ...
writeln( ... ).
至于回程航行,是不是像切换参数一样简单?因此,将travelLoop
重命名为travel_path
,然后将travel
重写为
travel(X,Y):- %// the to and fro loop
travel_path(X,Y,[X]), travel_path(Y,X,[Y]) -> true ; wrong.
wrong:- writeln("Wrong input. Try again."), nl.
答案 1 :(得分:0)
实际上下面是我程序的一部分,我们需要以菜单形式书写,所以这是菜单的一部分。我插入了你已经纠正的东西,我试过,我认为它有效,因为我认为所选择的任何2个目的地将有“是”的答案,因为有一个来回循环。但是,我认为它会影响选项(c)和(d)。所以我不确定矛盾在哪里。
choice(3):- write('Select the way: '),nl,nl,
write('a. Travel by using the same transportation.'),nl,
write('b. Travel by chaining together car, train, and plane journeys.'),nl,
write('c. How to travel from one place to another: cities.'),nl,
write('d. How to travel from one place to another: cities and transportation.'),nl,
read(Option),nl,nl,
write('Enter the place start from: '),read(Start),
write('Enter the place end at: '),read(End),
option(Option,Start,End),nl,nl.
option(a,A,B):- travelSimple(A,B),nl,write('yes'),nl,nl,menu.
option(b,A,B):- travel(A,B),nl,write('yes'),nl,nl,menu.
option(c,A,B):- findall(X,travel(A,B,X),List),nl,writelist(List),nl,nl,menu.
option(d,A,B):- findall(Y,travel1(A,B,Y),List1),nl,writelist(List1),nl,nl,menu.
option(_,_,_):- write('Wrong option input, please try again.'),nl,nl,menu.
travel1(X,Y):-byCar(X,Y).
travel1(Y,X):-byCar(X,Y).
travel2(P,Q):-byTrain(P,Q).
travel2(Q,P):-byTrain(P,Q).
travel3(M,N):-byPlane(M,N).
travel3(N,M):-byPlane(M,N).
travelSimple(X,Y):-byCar(X,Z),travel1(Z,Y);byCar(X,Y).
travelSimple(Y,X):-byCar(X,Z),travel1(Z,Y);byCar(X,Y).
travelSimple(P,Q):-byTrain(P,R),travel2(R,Q);byTrain(P,Q).
travelSimple(Q,P):-byTrain(P,R),travel2(R,Q);byTrain(P,Q).
travelSimple(M,N):-byPlane(M,L),travel3(L,N);byPlane(M,N).
travelSimple(N,M):-byPlane(M,L),travel3(L,N);byPlane(M,N).
travelSimple(_,_):-write('Wrong travel simple input, please try again.'),nl.
connect(X,Y,byCar):- byCar(X,Y) ; byCar(Y,X).
connect(X,Y,byTrain):- byTrain(X,Y) ; byTrain(Y,X).
connect(X,Y,byPlane):- byPlane(X,Y) ; byPlane(Y,X).
travel(X,Y):-travelLoop(X,Y,[X]).
travelLoop(X,Y,_):- connect(X,Y,byCar);
connect(X,Y,byTrain);
connect(X,Y,byPlane), X\=Y.
travelLoop(X,Y,Passed):-connect(X,Thru,byCar);
connect(X,Thru,byTrain);
connect(X,Thru,byPlane);
\+memberchk(Thru,Passed),travelLoop([Thru|Passed],Y,Thru).
travelLoop(X,Y,Path):- X=Y, writeln('Yes')-> true ; wrong.
travel(X,Y):-travel_Path(X,Y,[X]),
travel_Path(Y,X,[Y]) -> true ; wrong.
wrong:- writeln('Wrong travel input, please try again.'), nl.
travel(X,Y,go(X,Y)):-byCar(X,Y).
travel(X,Y,go(X,Y)):-byTrain(X,Y).
travel(X,Y,go(X,Y)):-byPlane(X,Y).
travel(X,Y,go(X,Z,W)):-travel(X,Z,go(X,Z)),travel(Z,Y,W).
travel1(X,Y,go(X,Y,car)):-byCar(X,Y).
travel1(X,Y,go(X,Y,train)):-byTrain(X,Y).
travel1(X,Y,go(X,Y,plane)):-byPlane(X,Y).
travel1(X,Y,go(X,Z,V,W)):-travel1(X,Z,go(X,Z,V)),travel1(Z,Y,W).
writelist([]).
writelist([L|Lt]):-write(L),nl,writelist(Lt).