Prolog中的递归-寻找城市之间的路径 [英] Recursion in Prolog - Finding Path Between Cities
问题描述
I'm trying to work my way through the exercises at the bottom of this page and I find myself utterly confused on number 3.
我们将获得以下旅行信息知识库:
We are given the following knowledge base of travel information:
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(singapore, auckland).
byPlane(losAngeles, auckland).
很容易找出是否有可能在两个城市之间旅行.我就是这样做的:
It's simple to find out if it's possible to travel between two cities. I just did this:
connected(X, Y) :- byCar(X, Y); byTrain(X, Y); byPlane(X, Y).
travel(X, Y) :- connected(X, Y).
travel(X, Z) :- connected(Y, Z), travel(X, Y).
但是,当我实际上必须使用变量统一路径时,我完全感到困惑!
However, when I have to actually unify the path with a variable, I am utterly confused!
我写了这个:
connected(X, Y) :- byCar(X, Y); byTrain(X, Y); byPlane(X, Y).
connected(Y, Z, Out) :- connected(Y, Z).
travel(X, Y, Out) :- connected(X, Y).
travel(A, Z, Out) :- connected(Y, Z),travel(A, Y, connected(Y, Z, Out)).
并称为travel(valmont, losAngeles,X).
在跟踪过程中,除了结尾处的匿名变量之外,还有一点显示正确的路径:
There is a point during the trace where the correct path shows up, aside from the anonymous variable at the end:
travel(valmont, metz, connected(metz, paris, connected(paris, losAngeles, _17)))
但是我实际上不知道如何用变量X
统一它!
but I don't actually know how to unify this with the variable X
!
我实在无法解决这个问题.谁能给我一个提示,将我推向正确的方向?是否只是我缺少的终止条件?
I can't really wrap my mind around this. Can anyone give me a hint just to push me in the right direction? Is there just a termination condition I'm missing or something?
现在我有
connected(X,Y) :- byCar(X,Y);byTrain(X,Y);byPlane(X,Y).
go(X,Y) :- connected(X,Y).
travel(X,Y,go(X,Y)) :- connected(X,Y).
travel(A,Z,Path) :- travel(Y,Z,Path),go(A,Y,Path).
go(A,Y,Path) :- travel(A,Y,Path).
但是它像这样被卡住了:
but it gets stuck like this:
4 4 Exit: byPlane(paris,losAngeles) ?
3 3 Exit: connected(paris,losAngeles) ?
2 2 Exit: travel(paris,losAngeles,go(paris,losAngeles)) ?
5 2 Call: go(metz,paris,go(paris,losAngeles)) ?
6 3 Call: travel(metz,paris,go(paris,losAngeles)) ?
7 4 Call: travel(_217,paris,go(paris,losAngeles)) ?
8 5 Call: travel(_242,paris,go(paris,losAngeles)) ?
9 6 Call: travel(_267,paris,go(paris,losAngeles)) ?
10 7 Call: travel(_292,paris,go(paris,losAngeles)) ?
我已经试过了,但是我无法用它来构建整个go(a,b,go(b,c))
等...
I've played around with it, but I can't get it to build the whole go(a,b,go(b,c))
etc...
推荐答案
我将为您介绍递归的基本情况:
I'll give you the base case of the recursion:
travel(X, Y, go(X, Y)) :- connected(X, Y).
递归情况看起来非常相似,除了您要构建的go/3
术语的头两个参数必须是位置,第二个路径是路径(另一个go/2
或go/3
术语).
The recursive case looks extremely similar, except that the go/3
term you're building must have locations as its first two arguments, and a path (another go/2
or go/3
term) as its second.
这篇关于Prolog中的递归-寻找城市之间的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!