52 349 Logic Programming

Practical 3


  1. Complete Practical 2 - at least to the point where you are happy with the practical material covered! There are important programming points hidden in the later exercises of that sheet. But, if you are running short of time, ensure that you at least give serious consideration to the exercises on this sheet.

  2. List handling. Write procedures which:

    1. concatenate two lists;
    2. delete single occurrences of some specified element from a list;
    3. use this deletion procedure to insert a specified element at some given place in an ordered list (e.g. to insert 'b' between 'a' and 'c'), and to give an alternative procedure for list membership;
    4. produce the reverse of a given list;
    5. delete multiple occurrences of a given element from a list; somewhat unusually, it's probably easier to start from scratch than it is to re-use the code from (b).

  3. Experiment with sorting algorithms such as those referred to in the lectures, to convince yourself that they work and to understand the thought processes involved in coding such algorithms.

  4. A set is simply a collection of items, in which an item either is or is not included; one obvious way of representing sets is by way of lists. Devise means of undertaking the principal set operations - which in Prolog terms of course means ascertaining the truth of a series of assertions about some set or sets.

  5. Experiment with the tree definitions introduced in the lectures. In particular, create a simple binary search tree, holding say names at the nodes. What happens if you apply 'write' to the tree?

    Develop code for pre-order and in-order traversals, perhaps using them in the context of printing out a succession of node values.

  6. Build a family tree (which, strictly speaking, isn't a tree at all .... but you can get away with using one), to be assembled from data of the form of that in royalty. What are the differences in building and in interrogation compared to a binary search tree?

    Note that running your code on royalty will probably fail due to a shortage of stack space! You need to incorporate a depth limit, which is more tricky .....

    If all else fails, reconsult exercise 3 of Practical 1!

  7. How might you define a graph (which is, remember, a set of vertices and a set of associated edges) in Prolog? Try using your representation to determine possible paths between two given nodes.

  8. Now, how about a menu interface which will repeatedly offer the user the choice between a number of possible problems, and return to the menu after execution of each? Again, this isn't quite as easy as it sounds, since you have to decide how to handle the failure of one of the predicates invoked from the menu.


© Paul Goldfinch 1996 D.C.G Addendum Return to 52 349 Menu