Logic Programming
06-25433
Autumn 2012

 

 

 

Lecture 9: Processing lists in Prolog 3

This lecture concentrates on accumulators - used to make programs more efficient by cutting out unnecessary extra steps.

This lecture include:

  • using accumulators to write more efficient (and especially tail-recursive) procedures;
  • writing list processing rules that are called from within list processing rules;
  • doing the "impossible" - adding to the end of a list;
  • rewriting procedures to make them more efficient - by adding extra arguments.
How to prepare

  At a general level of list processing (rather than specifically in Prolog), think about how you would:
  1. how you could copy a list, member by member:
    1. so the new list is in the same order as the old list
    2. so the new list is in the reverse order of the old list;
  2. given a list which is made up of a mixture of atomics or lists (which are made up of a mixture of atomics or lists (which are made up of a mixture of atomics or lists ( etc ))), output each atomic in the order they occur in the list. So, the list:
    [[a,b],e,[[f]]]
    would be output as:
    a b e f

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 - copying a list element-by-element (order preserving) Example 2 - appending lists Example 2 - with extra demo code
Call as app/3
Example 3 - reversing a list (faulty) Example 4 - reversing a list (with an accumulator) Example 4 - with extra demo code
Call as reverse_acc/2
Example 5 - reversing a list (inefficiently) Example 5 - with extra demo code
Call as reverse_naive/2
Example 6 - flattening a list (inefficiently)
Example 6 - with extra demo code
Call as flatten/2
Example 7 - flattening a list (efficiently)  

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