Prolog W5

Prolog Practical Session - Week 5


Task 1 (easy)

Write a Prolog program write_list(L) so that the items in L are pinted on screen, one by one.

In Prolog, write(X) is a built-in predicate. It simplys display X as it is.

For example,

 ?- write(['Hello', world, '!']).  

displays

 [Hello, world, !]

But your write_list(['Hello', world, '!']) should display

 Hello world !

Task 2 (harder)

Write a program cal_route(DirList, DistList, NewDirList, NewDistList) which simplifies a route instruction described in DirList and DistList.

DirList is a list of given directions, and DistList is a list of distances.

For example, if we have

DirList = [east, east, south, south] 
DistList = [1,    10,   20,   5]
it means to go to east 1 metre, carry on east direction for another 10 metres, then turn to south for 20 metres, then 5 more metres in same direction.

In this case, it would be better to reduce the instruction lists to

[east, west] 
[11,   25]

Your cal_route(DirList, Distances, NewDirList, NewDistances) implements this function.

For example,

?- cal_route([a,a,b,c,c],[1,2,3,4,5], D1, D2). 
returns

D1 = [a,b,c]

D2 = [3,3,9]


Submit your code with test results to Rong Yang after you finish the task. This work is worth of 10% this term's assignment.


Rong Yang,