1.  Street map information may be sorted in a Prolog program by labeling junctions with numbers and dividing up streets with junctions along their length into parts labeled A, B, C, etc., thus:

 
 

Here, Republic street is divided up into two parts, A and B, because of the junction with South street (junction number 6). Separate Prolog facts are then used to represent each of the following pieces of information:

Republic Street A connects 3 to 6 and is 100 metres long.

Republic Street B connects 6 to 7 and is 150 metres long.

South Street B connects 5 to 6 and is 50 metres long. Etc…

  1. Device a representation in Prolog as suggested above to describe all the facts given in the map.
  2. Hence write a Prolog program which, on being issued a goal of the form:

  3.  

     

    find_route(Junction1, Junction2, Route_List, Distance),

    has as its solutions the possible routes between Junction1 and Junction2. In each individual solution, Route_List should be bound to a list indicating the path, and Distance should be bound to the total distance of the path. For example, one of the solutions to find_route(1,3,Route,Dist) should be:

    Route =[go(1,4,MerchantA,90),go(4,5,SouthB,50),go(5,2,Zacchary,100),go(2,3,MelitaA,50)]

    Dist = 290

  4. Now extend your Prolog program to compute the shortest route.
HINT: Find a route and see if there is one that is shorter than it.

2.  In Prolog, a set of integers can be represented using a list. The lists [2,3,1] and [3,1,2] are both valid representations of a set having elements 1, 2, and 3: the order of elements is not regarded as significant. In a set, no item occurs more than once, so if an item is added to a set of which its already an element, then the set remains unchanged. Write a Prolog program in which it is possible to issue goals of the forms indicated below. You will find it useful to define an is_a_member_of predicate first of all:
  1. add_element(Element, Set, NewSet), which binds NewSet to the result of adding the new element Element to the set Set. Your code should take into consideration the fact that only unique elements are allowed in a set.
  2. cardinality(Set,Number), which binds the number of elements in set Set to Number. E.g. cardinality([a,b,c,d,e],Size) should bind Size to 5.
  3. equal(Set1, Set2), which should succeed if Set1 and Set2 contain the same elements (not necessarily in the same order).
(HINT: Two sets are equal if they have the same cardinality and …)


3.  A recursive predicate has one of its goals that calls itself until a stopping condition is encountered.
  1. Write clauses for a predicate sum_up_to(Limit, Sum), which computes the Sum of the integers between 1 and the Limit inclusive.

  2.  

     

    e.g. sum_up_to(5, Ans) binds Ans to 15 ( = 1+2+3+4+5)

  3. Consider the following program which deletes a single occurrence of an item from a list:

  4.  

     

    del(X,[X|T],T).

    del(X,[H|T],[H|T1]) :- del(X,T,T1).

    Write clauses for a predicate delete_all(Item, List, Final_List), which deletes all occurrences of the Item from the List to give the Final_List.

  5. Write clauses for a predicate product_of(List,Product), which works out the Product of all the integer items with the List.
e.g. product_of([4,2,6],Ans) binds Ans to 48 ( = 4 x 2 x 6)