nodes - Prolog - Need to understand what a rule is doing -
nodes - Prolog - Need to understand what a rule is doing -
i have database total of facts such as:
overground( watfordjunction , watfordhighstreet , 2 ). overground( watfordhighstreet , bushey , 3 ). overground( bushey , carpenderspark , 3 ). overground( carpenderspark , hatchend , 2 ).
example: watford junction watfordhighstreet takes 2 minutes.
i designed rule can test if journey station can done including reverse journeys.
isjourney(station1,station2):- overground(station1,_,_), overground(_,station2,_),!; overground(station2,_,_), overground(_,station1,_),!. isjourney(station1,station2):- overground(station1,station3,_), isjourney(station3,station2). isjourney(station1,station2):- overground(station3,station2,_), isjourney(station1,station3).
i understand 1st line first checks if station1 , station2 exist in facts. cuts end backtracking 1 time journey found true in order prevent infinite loop.
the 2nd line checks if journey between station1 , station2 possible through intermediate station (station3). i'm confused 3rd line... seems me doing opposite, in other words checking same thing line 2 reverse journey. find if delete 3rd line , test code still works, including if test reverse journey. if sec line of rule can check if forwards , reverse journey possible, 3rd line doing?
it seems me doing opposite, in other words checking same thing line 2 reverse journey.
that's indeed seems doing. code confusing; improve have been written using intermediate predicate such as
connected(a, b) :- overground(a, b, _). connected(a, b) :- overground(b, a, _).
prolog nodes rule
Comments
Post a Comment