How can I write rules for the following questions and facts in Prolog?

送分小仙女□ 提交于 2019-12-25 08:36:07

问题


  1. Write a Prolog rule to find any route between two stations.

    route(Station1, Station2, Route)

where Station1 and Station2 are station names and Route is a list of stations connecting Station1 and Station2 in order, including both Station1 and Station2.

  1. Write a Prolog rule to find the time taken to travel on a route between two stations, on the assumption that the time to travel between adjacent stations is 4 minutes. Ignore any time associated with changing lines at interchanges.

    route_time(Station1, Station2, Route, Minutes)

where Station1 and Station2 are station names, Route is a list of stations connecting Station1 and Station2 in order, including both Station1 and Station2, and Minutes is the time taken.

station(al,[metropolitan]).
station(ls,[metropolitan,central]).
station(kx,[metropolitan,victoria]).
station(bs,[metropolitan]).
station(fr,[metropolitan]).
station(ke,[northern]).
station(em,[northern,bakerloo]).
station(tc,[northern,central]).
station(ws,[northern,victoria]).
station(eu,[northern]).
station(wa,[bakerloo]).
station(pa,[bakerloo]).
station(oc,[bakerloo,central,victoria]).
station(ec,[bakerloo]).
station(nh,[central]).
station(lg,[central]).
station(cl,[central]).
station(bg,[central]).
station(br,[victoria]).
station(vi,[victoria]).
station(fp,[victoria]).


/* Facts about the direct connection between two stations.  
   The adjacent rule shows the connections between two stations */

adjacent1(X,Y):- adjacent(X,Y);adjacent(Y,X).

/* The first rule is X is adjacent Y IF and only IF   
   X is adjacent to Y and Y is adjacent to X */


/* Bakerloo Line adjacents*/

adjacent(em,ec).
adjacent(em,ke).
adjacent(em,tc).
adjacent(em,oc).
adjacent(oc,vi).
adjacent(oc,ws).
adjacent(oc,pa).
adjacent(oc,lg).
adjacent(oc,tc).
adjacent(pa,wa).


/* Central Line Adjacents*/

adjacent(bg,ls).
adjacent(ls,al).
adjacent(ls,kx).
adjacent(ls,cl).
adjacent(cl,tc).
adjacent(lg,nh).


/*Metro adjacents*/

adjacent(kx,fp).
adjacent(kx,ws).
adjacent(kx,bs).
adjacent(bs,fr).


 /* Northern Line*/

adjacent(tc,ws).
adjacent(ws,eu).


/*Victoria line */

adjacent(br,vi).

来源:https://stackoverflow.com/questions/41742556/how-can-i-write-rules-for-the-following-questions-and-facts-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!