我无法解决Learn Prolog Now练习(this page中的最后一个)。简而言之:有一堆事实的格式为“byCar(place1,place2)”,“byTrain(place1,place2)”和“byAirplane(place1,place2)”,我必须写一个travel / 3谓词(我称之为门票)告诉你从哪个地方购买哪些门票从地方1到地方2。
例如,查询
tickets(valmont, losAngeles, X)
应该返回这样的答案:
X = goByCar(valmont, saarbruecken,
goByTrain(saarbruecken, frankfurt,
goByAirplane(frankfurt, bangkok,
goByAirplane(bangkok, auckland,
goByAirplane(auckland, losAngeles)))))
我目前的解决方案是:
ticketsDir(A, B, goByCar(A, B)) :- byCar(A, B).
ticketsDir(A, B, goByTrain(A, B)) :- byTrain(A, B).
ticketsDir(A, B, goByAirplane(A, B)) :- byAirplane(A, B).
swapGo(goByCar(A, B)) :- goByCar(B, A).
swapGo(goByTrain(A, B)) :- goByTrain(B, A).
swapGo(goByAirplane(A, B)) :- goByAirplane(B, A).
mergeGo(goByCar(A, B), Go2) :- goByCar(A, B, Go2).
mergeGo(goByTrain(A, B), Go2) :- goByTrain(A, B, Go2).
mergeGo(goByAirplane(A, B), Go2) :- goByAirplane(A, B, Go2).
tickets(A, B, Go) :- ticketsDir(A, B, Go).
tickets(A, B, swapGo(Go)) :- ticketsDir(B, A, Go).
tickets(A, B, mergeGo(Go1, Go2)) :- ticketsDir(A, Z, Go1), tickets(Z, B, Go2).
tickets(A, B, mergeGo(Go1, Go2)) :- ticketsDir(Z, A, Go1), tickets(Z, B, Go2).
但是,该查询返回此结果:
X = mergeGo(goByCar(valmont, saarbruecken),
mergeGo(goByTrain(saarbruecken, frankfurt),
mergeGo(goByAirplane(frankfurt, bangkok),
mergeGo(goByAirplane(bangkok, auckland),
swapGo(goByAirplane(losAngeles, auckland))))))"
现在奇怪的是,查询
swapGo(goByCar(A, B))
返回:
Error: swapGo/1: Undefined procedure: goByCar/2
和查询
mergeGo(goByCar(A, B), go2)
返回:
Error: mergeGo/2: Undefined procedure: goByCar/3
好像我正在定义“swapGo”和“mergeGo”错误,或者可能是“门票”。有什么建议可以解决吗?谢谢!