我目前正在编写一个递归 Prolog 程序,将路线连接起来,以创建伯明翰地区的基本 GPS。目前,我的输出如下:
输入 (rù kǒu)
routeplan(selly_oak, aston, P).
输出
P = [selly_oak, edgbaston, ... , aston]
我想做的是让我的程序提供某种界面,这样如果我输入类似于以下内容的东西:
Route from selly_oak to aston
它会给我提供:
Go from selly_oak to edgbaston
Go from edgbaston to ...
Finally, Go from ... to aston.
Prolog是一种强大的语言,因此我认为这很容易实现,但我借阅的许多书似乎都跳过了这一部分。据我所知,我必须使用类似于write()和read()的内容,尽管细节对我来说是未知的。
有没有人可以给一个Prolog初学者提供一些基本的例子或进一步的信息链接?
编辑:很多答案看起来很复杂,但实际上解决方案只需要5-10行代码。读取值不是问题,我可以通过以下方式处理:
find:-
write( Where are you? ),
read(X),
nl, write( Where do you want to go? ),
read(Y),
loopForRoute(X,Y).
我倾向于使用write()将输出写出,这样就可以使用换行符(nl),以便像上面的输出一样显示。
如果这是我的输入,我该如何安排顶部的routeplan()函数来处理这些输入?另外,如果我想将这些站点的线路作为额外的参数添加,该如何实现?所有链接都在文件的开头被定义,就像这样:
rlinks(selly_oak, edgbaston, uob_line).
rlinks(edgbaston, bham_new_street, main_line).
因此,有了这些信息,能够把这条线读出来是很好的。
Go from selly_oak to edgbaston using the uob_line
Go from edgbaston to ... using the ...
Finally, go from ... to aston using the astuni_line