0% found this document useful (0 votes)
38 views16 pages

An Introduction To Prolog Programming: Ulle Endriss Institute For Logic, Language and Computation University of Amsterdam

This document discusses list manipulation in Prolog. It explains that lists are represented internally as compound terms with the dot (.) functor and the empty list [] as the innermost term. It introduces common list patterns like head/tail and using the bar (|) notation to separate the head from the tail. Built-in predicates for lists are also covered, including append/3 for concatenating lists and member/2 for checking list membership. The document provides examples of using these patterns and predicates to implement functions like iterating through a list and reversing a list.

Uploaded by

Boroka Csete
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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views16 pages

An Introduction To Prolog Programming: Ulle Endriss Institute For Logic, Language and Computation University of Amsterdam

This document discusses list manipulation in Prolog. It explains that lists are represented internally as compound terms with the dot (.) functor and the empty list [] as the innermost term. It introduces common list patterns like head/tail and using the bar (|) notation to separate the head from the tail. Built-in predicates for lists are also covered, including append/3 for concatenating lists and member/2 for checking list membership. The document provides examples of using these patterns and predicates to implement functions like iterating through a list and reversing a list.

Uploaded by

Boroka Csete
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 PDF, TXT or read online on Scribd
You are on page 1/ 16

List Manipulation

LP&ZT 2005

An Introduction to Prolog Programming


Ulle Endriss Institute for Logic, Language and Computation University of Amsterdam

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Lists in Prolog
One of the most useful data structures in Prolog are lists. The objective of this lecture is to show you how lists are represented in Prolog and to introduce you to the basic principles of working with lists. An example for a Prolog list: [elephant, horse, donkey, dog] Lists are enclosed in square brackets. Their elements could be any Prolog terms (including other lists). The empty list is []. Another example: [a, X, [], f(X,y), 47, [a,b,c], bigger(cow,dog)]

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Internal Representation
Internally, the list [a, b, c] corresponds to the term .(a, .(b, .(c, []))) That means, this is just a new notation. Internally, lists are just compound terms with the functor . (dot) and the special atom [] as an argument on the innermost level. We can verify this also in Prolog: ?- X = .(a, .(b, .(c, []))). X = [a, b, c] Yes
Ulle Endriss ([email protected]) 3

List Manipulation

LP&ZT 2005

The Bar Notation


If a bar | is put just before the last term in a list, it means that this last term denotes a sub-list. Inserting the elements before the bar at the beginning of the sub-list yields the entire list. For example, [a, b, c, d] is the same as [a, b | [c, d]].

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Examples
Extract the second element from a given list: ?- [a, b, c, d, e] = [_, X | _]. X = b Yes Make sure the rst element is a 1 and get the sub-list after the second element: ?- MyList = [1, 2, 3, 4, 5], MyList = [1, _ | Rest]. MyList = [1, 2, 3, 4, 5] Rest = [3, 4, 5] Yes

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Head and Tail


The rst element of a list is called its head. The rest of the list is called its tail. (The empty list doesnt have a head.) A special case of the bar notation with exactly one element before the bar is called the head/tail-pattern. It can be used to extract head and/or tail from a list. Example: ?- [elephant, horse, tiger, dog] = [Head | Tail]. Head = elephant Tail = [horse, tiger, dog] Yes

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Head and Tail (cont.)


Another example: ?- [elephant] = [X | Y]. X = elephant Y = [] Yes Note: The tail of a list is always a list itself. The head of a list is an element of that list. The head could also be a list itself (but it usually isnt).

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Appending Lists
We want to write a predicate concat_lists/3 to concatenate (append) two given lists. It should work like this: ?- concat_lists([1, 2, 3, 4], [dog, cow, tiger], L). L = [1, 2, 3, 4, dog, cow, tiger] Yes

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Solution
The predicate concat_lists/3 is implemented recursively. The base case is when one of the lists is empty. In every recursion step we take o the head and use the same predicate again, with the (shorter) tail, until we reach the base case. concat_lists([], List, List). concat_lists([Elem|List1], List2, [Elem|List3]) :concat_lists(List1, List2, List3).

Ulle Endriss ([email protected])

List Manipulation

LP&ZT 2005

Do More
Amongst other things, concat_lists/3 can also be used for decomposing lists: ?- concat_lists(Begin, End, [1, 2, 3]). Begin = [] End = [1, 2, 3] ; Begin = [1] End = [2, 3] ; Begin = [1, 2] End = [3] ; Begin = [1, 2, 3] End = [] ; No

Ulle Endriss ([email protected])

10

List Manipulation

LP&ZT 2005

Built-in Predicates for List Manipulation


append/3: Append two lists (same as our concat_lists/3). ?- append([1, 2, 3], List, [1, 2, 3, 4, 5]). List = [4, 5] Yes length/2: Get the length of a list. ?- length([tiger, donkey, cow, tiger], N). N = 4 Yes

Ulle Endriss ([email protected])

11

List Manipulation

LP&ZT 2005

Membership
member/2: Test for membership. ?- member(tiger, [dog, tiger, elephant, horse]). Yes Backtracking into member/2: ?- member(X, [dog, tiger, elephant]). X = dog ; X = tiger ; X = elephant ; No

Ulle Endriss ([email protected])

12

List Manipulation

LP&ZT 2005

Example
Consider the following program: show(List) :member(Element, List), write(Element), nl, fail. Note: fail is a built-in predicate that always fails. What happens when you submit a query like the following one? ?- show([elephant, horse, donkey, dog]).

Ulle Endriss ([email protected])

13

List Manipulation

LP&ZT 2005

Example (cont.)
?- show([elephant, horse, donkey, dog]). elephant horse donkey dog No The fail at the end of the rule causes Prolog to backtrack. The subgoal member(Element, List) is the only choicepoint. In every backtracking-cycle a new element of List is matched with the variable Element. Eventually, the query fails (No).

Ulle Endriss ([email protected])

14

List Manipulation

LP&ZT 2005

More Built-in Predicates


reverse/2: Reverse the order of elements in a list. ?- reverse([1, 2, 3, 4, 5], X). X = [5, 4, 3, 2, 1] Yes More built-in predicates can be found in the reference manual.

Ulle Endriss ([email protected])

15

List Manipulation

LP&ZT 2005

Summary: List Manipulation


List notation: normal: [Elem1, Elem2, Elem3] (empty list: []) internal: .(Elem1, .(Elem2, .(Elem3, []))) bar notation: [Elem1, Elem2 | Rest] head/tail-pattern: [Head | Tail] Many predicates can be implemented recursively, exploiting the head/tail-pattern. Built-in predicates: append/3, member/2, length/2, . . .

Ulle Endriss ([email protected])

16

You might also like