【问题标题】:How to check if a station exists out of facts of stations and lines in PROLOG如何根据PROLOG中的车站和线路的事实检查车站是否存在
【发布时间】:2020-05-22 09:07:20
【问题描述】:

我写了以下事实来表示管图

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]).

我需要写一个form的谓词

station_exists(Station)

检查一个站是否存在,但我不知道如何编写规则。我尝试过类似的方法:

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

但对于任何电台名称,它都会返回 true。有人可以帮忙吗?

【问题讨论】:

  • 你的常量以大写字母开头,所以 Prolog 认为这些是变量而不是常量。
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 3.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?

标签: prolog predicate


【解决方案1】:

您的常量以大写字母开头,因此 Prolog 认为这些是变量而不是常量。

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

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]).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多