Search code examples
prologpredicate

How to check if a station exists out of facts of stations and lines in PROLOG


I have written the following facts to represent a tube map

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

I need to write a predicate of form

station_exists(Station)

to check if a station exists, but I can't figure out how to write the rule. I've tried something like:

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

But it returns true for any station name. Can anyone help?


Solution

  • Your constants start with an uppercase, so Prolog considers these to be variables not constants.

    You should rewrite these to start with an uppercase, or a quoted atom:

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