如何检查PROLOG中站点和线路的事实是否存在站点

问题描述 投票:1回答:1

我已写出以下事实来表示管图

station(AL,[Metropolitan]).
station(BG,[Central]).
station(BR,[Victoria]).
station(BS,[Metropolitan]).
station(CL,[Central]).
station(EC,[Bakerloo]).
station(EM,[Bakerloo,Northern]).
station(EU,[Northern]).
station(FP,[Victoria]).
station(FR,[Metropolitan]).
station(KE,[Northern]).
station(KX,[Metropolitan,Victoria]).
station(LG,[Central]).
station(LS,[Central,Metropolitan]).
station(NH,[Central]).
station(OC,[Bakerloo,Central,Victoria]).
station(PA,[Bakerloo]).
station(TC,[Central,Northern]).
station(VI,[Victoria]).
station(WA,[Bakerloo]).
station(WS,[Northern,Victoria]).

我需要写一个形式谓词

station_exists(Station)

检查工作站是否存在,但我不知道如何编写规则。我已经尝试过类似的东西:

station_exists(Station):- station(Station,_)

但是对于任何工作站名称,它都返回true。谁能帮忙?

prolog predicate
1个回答
1
投票

您的常量以大写字母开头,因此Prolog认为它们是variables不是常量。

您应该重写它们以大写字母或带引号的原子开头:

station(al, [metropolitan]).
station(bg, [central]).
station(br, [victoria]).
station(bs, [metropolitan]).
station(cl, [central]).
station(ec, [bakerloo]).
station(em, [bakerloo,northern]).
station(eu, [northern]).
station(fp, [victoria]).
station(fr, [metropolitan]).
station(ke, [northern]).
station(kx, [metropolitan,victoria]).
station(lg, [central]).
station(ls, [central,metropolitan]).
station(nh, [central]).
station(oc, [bakerloo,central,victoria]).
station(pa, [bakerloo]).
station(tc, [central,northern]).
station(vi, [victoria]).
station(wa, [bakerloo]).
station(ws, [northern,victoria]).
© www.soinside.com 2019 - 2024. All rights reserved.