0% found this document useful (0 votes)
30 views19 pages

Elementary List Processing

This lecture covers searching graphs and trees, unification of lists including lists within lists, and basic patterns of recursively processing lists in Prolog. It discusses representing graphs as nodes and arcs, searching graphs using recursion similarly to finding friends or family members, and unifying lists by matching heads and tails. It also presents three common patterns of recursive list processing: terminating at the end of the list, when a given element is found, and when a given number of elements have been scanned.

Uploaded by

vytor_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views19 pages

Elementary List Processing

This lecture covers searching graphs and trees, unification of lists including lists within lists, and basic patterns of recursively processing lists in Prolog. It discusses representing graphs as nodes and arcs, searching graphs using recursion similarly to finding friends or family members, and unifying lists by matching heads and tails. It also presents three common patterns of recursive list processing: terminating at the end of the list, when a given element is found, and when a given number of elements have been scanned.

Uploaded by

vytor_
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

06-02630 – Software Workshop Prolog

Elementary list processing

This lecture will cover:


– searching trees and graphs
– unification of lists, including the
unification of lists within lists;
– three basic patterns of processing lists.
06-02630 – Software Workshop Prolog

Searching graphs - 1

We can represent a graph by a set of arcs between


two nodes:
path(a,1). path(a,3).
  1 2
5
path(1,2). path(1,4). 4
a
 
path(2,5). path(3,4). 3
 
path(4,5).

4 - Elementary list processing 2


06-02630 – Software Workshop Prolog

Searching graphs - 2
Searching this graph is essentially the same problem
as finding neighbours and friends and searching
family trees:
 
% 1 - boundary
route(Start, End) :-
path(Start, End).
% 2 - recursive
route(Start, End) :-
path(Start, Via),
route(Via, End).

4 - Elementary list processing 3


06-02630 – Software Workshop Prolog

Searching graphs - 2
Searching this graph is essentially the same problem as
finding neighbours and friends and searching family trees:
 

% 1 - boundary
friend(Start, End) :-
path(Start, End).
% 2 - recursive
friend(Start, End) :-
path(Start, Via),
friend(Via, End).

4 - Elementary list processing 4


06-02630 – Software Workshop Prolog

Searching graphs - 2
Searching this graph is essentially the same problem as
finding neighbours and friends and searching family trees:
 

% 1 - boundary
friend(Start, End) :-
friend_of(Start, End).
% 2 - recursive
friend(Start, End) :-
friend_of(Start, Via),
friend(Via, End).

4 - Elementary list processing 5


06-02630 – Software Workshop Prolog

Searching graphs - 2
Searching this graph is essentially the same problem as
finding neighbours and friends and searching family trees:
 

% 1 - boundary
friend(Person, Friend) :-
friend_of(Person, Friend).
% 2 - recursive
friend(Person, Friend) :-
friend_of(Person, Inter),
friend(Inter, Friend).

4 - Elementary list processing 6


06-02630 – Software Workshop Prolog

Searching graphs - 3
As this is the same problem as finding neighbours
and friends, we’ve seen how Prolog will search for
solutions.
 
But, what happens if we remove a path, eg:
path(2,5).
and then use the goal:
| ?- route(a, 5).
 

4 - Elementary list processing 7


06-02630 – Software Workshop Prolog

Unification of lists - 1
Lists are a variable length data structure
– therefore we have to have a method of processing
that doesn’t require us to know how long a list is in
advance.
Method
Refer to the head and tail of a list. In Prolog, this is
written as:
[Head|Tail]
where “|” is the list constructor.

4 - Elementary list processing 8


06-02630 – Software Workshop Prolog

Unification of lists - 2
Simple examples of unification are:
| ?- [a,b,c] = [Head|Tail].
 
Head = a
Tail = [b,c]
 
Note the tail is always a list – even if it is empty:
| ?- [a] = [Head|Tail].
 
Head = a
Tail = []

4 - Elementary list processing 9


06-02630 – Software Workshop Prolog

Unification of lists - 3
We can also specify more than one head:

| ?- [a,b,c] = [Hd1,Hd2|Tail].
 
Hd1 = a
Hd2 = b
Tail = [c]

4 - Elementary list processing 10


06-02630 – Software Workshop Prolog

Patterns of recursion:
Terminating at the end of the list
% 1 - terminating
len_of_list([], Length, Length).
% 2 - recursive
len_of_list([_Head|Tail],
Length1, Length) :-
Length2 is Length1 + 1,
len_of_list(Tail, Length2, Length).

| ?- len_of_list([a,b,c], 0, L).

L = 3 ;
no

4 - Elementary list processing 11


06-02630 – Software Workshop Prolog

Patterns of recursion:
Terminating when given element is found

For instance finding a given element:


 
% 1 - terminating
memb(Elem, [Elem|_]).
% 2 - recursive
memb(Elem, [_|Tail]) :-
memb(Elem, Tail).

| ?- memb(b, [a,b,c]).
 
yes

4 - Elementary list processing 12


06-02630 – Software Workshop Prolog

Patterns of recursion:
Terminating when given element is found

Notice, this can be run “backwards” to enumerate the


individual members of a list:

| ?- memb(Item, [a,b]).
 
Item = a ;
Item = b ;
no

4 - Elementary list processing 13


06-02630 – Software Workshop Prolog

Patterns of recursion:
Terminating when given number of
elements have been scanned - 1

% 1 – recursive
nth(Count, Item, [_|Tail]) :-
Count > 1,
Count0 is Count - 1,
nth(Count0, Item, Tail).
% 2 – terminating
nth(1, Head, [Head|_]).

4 - Elementary list processing 14


06-02630 – Software Workshop Prolog

Patterns of recursion:
Terminating when given number of
elements have been scanned - 2

In this example, the code counts down from the given


position to 1.

| ?- nth(3,Item,[a,b,c,d]).

Item = c ;

no

4 - Elementary list processing 15


06-02630 – Software Workshop Prolog

Sundry examples - 1
– What is the terminating pattern?
– Can it be run “backwards”?

% 1 - terminating
app([], List, List).
% 2 - recursive
app([Head|L1],L2,[Head|L3]) :-
app(L1, L2, L3).

4 - Elementary list processing 16


06-02630 – Software Workshop Prolog

Sundry examples - 2
– What is the terminating pattern?
– Does running this “backwards” make it insert?

% 1 - terminating
delete(Head,[Head|Tail],Tail).
% 2 - recursive
delete(Item, [Head|Tail],
[Head|New_Tail]) :-
delete(Item, Tail, New_Tail).

4 - Elementary list processing 17


06-02630 – Software Workshop Prolog

Summary - 1
Prolog uses recursion widely to do interesting things.

The “family tree” pattern of recursion is very


common - it is an example of singly recursive
procedures.

All examples in this lecture have the “family tree”


pattern of recursion.

4 - Elementary list processing 18


06-02630 – Software Workshop Prolog

Summary - 2
The list processing procedures can be classified
according to termination criterion:
– empty list
– specified member
– specified position

All list processing is only an elaboration of what is


in this lecture.

4 - Elementary list processing 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy