簡體   English   中英

從PROLOG中的列表中獲取成員

[英]Getting members from a list in PROLOG

我必須制定一條規則以返回輸入站的線路

但是,為了獲得所有的行,我必須制定一條規則,例如:

line(Line,ListofStations) :-  
    station(ListofStations,[Line]);
    station(ListofStations,[_,Line]);
    station(ListofStations,[Line,_]);
    station(ListofStations,[Line,_,_]);
    station(ListofStations,[_,Line,_]);
    station(ListofStations,[Line,_,_]). 

我需要使用謂詞來使規則變小,但成員函數僅返回true,並且以下規則適用於所有情況,但在列表中具有多個元素的情況除外。

line(Line,ListofStations):- station(ListofStations,[Line]).

事實:

station(oxford_circus,[bakerloo,central,victoria]). 
station(embankment,[bakerloo,northern]). 
station(elephantandcastle,[bakerloo]).
station(nottingHill_gate,[central]). 
station(lancaster_gate,[central]).
station(tottenham_court_road,[central]). 
station(chancery_lane,[central]). 
station(liverpool_street,[central,metropolitan]). 
station(bethnal_green,[central]).

不確定是要站點還是站點站點,但是...在兩種情況下,我認為您都需要member/2謂詞(據我所知,這是SWI-Prolog支持的ISO)。

我建議類似

line(Line, ListofStations) :-
  station(ListofStations, LineList),
  member(Line, LineList).

雙向均可使用。

舉例來說,當我打電話

 line(central, L)

我得到( L是統一的) oxford_circusnottingHill_gatelancaster_gatetottenham_court_roadchancery_laneliverpool_streetbethnal_green

當我打電話時

line(L, oxford_circus)

我到centralvictoriabakerloo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM