Logic Programming
06-25433
Autumn 2012

 

 

 

Lecture 8: Processing lists in Prolog 2

This lecture shows that techniques introduced before (analysing terminating conditions and recursive programming) can be used to develop more complex procedures.

This lecture include:

  • writing procedures with one or more terminating or recursive clauses;
  • one/all deletions - deleting one or all instances of an element from a list;
  • the effects of matching v. unification;
  • changing the order in which solutions are presented by changing clause order.
How to prepare

  At a general level of list processing (rather than specifically in Prolog), think about how you would:
  1. Append two lists to form a third, longer list, e.g:
    [a,b,c], [1,2,3][a,b,c,1,2,3]
  2. Delete one instance of a given term from a list, e.g. delete a:
    [a,b,c,a,b,c,a,b,c][b,c,a,b,c,a,b,c]
    [a,b,c,a,b,c,a,b,c][a,b,c,b,c,a,b,c]
    [a,b,c,a,b,c,a,b,c][a,b,c,a,b,c,b,c]
  3. Delete either one or all instances of a given term from a list, e.g. delete a:
    [a,b,c,a,b,c,a,b,c][b,c,b,c,b,c]
Think about how, in Prolog, you could change the order in which a given term was deleted from a list - so that the instance deepest in the list is the first to be removed:
[a,b,c,a,b,c,a,b,c][a,b,c,a,b,c,b,c]
[a,b,c,a,b,c,a,b,c][a,b,c,b,c,a,b,c]
[a,b,c,a,b,c,a,b,c][b,c,a,b,c,a,b,c]

Video

  Recording of the lecture (Vimeo)

Slides

  If you really want the slides from the lecture ... but don't forget printing the slides is not the same as understanding them.

Summary

  Summary and programs with commentary from the lecture

Programs from the lecture

 
Example 1 - length of a list Example 2 - membership of a list Example 3 - finding the nth member
Example 4 - Classifying elements of a list Example 5 - Pairing elements from two lists (faulty) Example 6 - Pairing elements from two lists (corrected)
Example 6 - Example 6 with extra demo code
Call as pair/3
Example 7 - Delete an element from a list Example 7 - Example 7 with extra demo code
Call as delete_1/3
Example 8 - Delete only one element from a list Example 9 - Delete all instances of an element from a list Example 10 - Matching instead of unification
Call as delete_1/3
Example 10 - Delete only one element from a list
With the order of clauses reversed
Example 10 - Example 11 with extra demo code
With the order of clauses reversed
Call as delete_1/3
 

Individual study suggestions and further reading

 

Amongst the less elementary topics on lists to be studied include:

Less elementary topics in list processing are covered in:

The best source on advanced list processing techniques (i.e. beyond the scope of this lecture) is:

Assessed work associated with this lecture

  Tutorial sheet 4