在prolog中查找一个国家所有机场的列表
这是我的flightdb.pl在prolog中查找一个国家所有机场的列表,prolog,Prolog,这是我的flightdb.pl #flight(citya ,cityb) flight(dublin, london). flight(cork, moscow). flight(chicago, dublin). flight(berlin, moscow). flight(cork, newyork). flight(paris, hongkong). #country(city,country) country(dublin,ireland). country(cork,ireland
#flight(citya ,cityb)
flight(dublin, london).
flight(cork, moscow).
flight(chicago, dublin).
flight(berlin, moscow).
flight(cork, newyork).
flight(paris, hongkong).
#country(city,country)
country(dublin,ireland).
country(cork,ireland).
country(london,uk).
country(rome,italy).
country(moscow,russia).
country(hongkong,china).
country(amsterdam,holland).
country(berlin,germany).
country(paris,france).
country(newyork,usa).
country(chicago,usa).
country(sao_paulo,brazil).
country(rio,brazil).
我正在设法弄到这个国家的所有机场
这是我写的谓词
list_airport(X,L) :-country(L,X).
我刚刚开始使用prolog,逻辑正确吗?您可以通过以下方式查询数据库:
?- country(X, ireland).
X = dublin ;
X = cork.
您可以通过以下方式生成所有匹配项的列表:
因此,如果您想获得给定国家的城市列表,可以使用:
list_airport(C, L) :-
findall(Ci, country(Ci, C), L).
并通过以下方式进行查询:
?- list_airport(usa, L).
L = [newyork, chicago].
@甘道夫:你的
list\u airport/2
不会返回列表,而是将L
与airpoirts统一起来。这本身并没有错。但是如果是这样的话,你实际上不需要定义一个list\u airport/2
谓词,因为list\u airport/2
只是country/2
的一个“交换”版本。嗨,你能回答这个问题吗@威廉·范昂森
?- list_airport(usa, L).
L = [newyork, chicago].