An Introduction To Prolog Programming: Ulle Endriss Institute For Logic, Language and Computation University of Amsterdam
An Introduction To Prolog Programming: Ulle Endriss Institute For Logic, Language and Computation University of Amsterdam
LP&ZT 2005
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)]
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
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
List Manipulation
LP&ZT 2005
List Manipulation
LP&ZT 2005
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
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).
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
10
List Manipulation
LP&ZT 2005
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
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]).
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).
14
List Manipulation
LP&ZT 2005
15
List Manipulation
LP&ZT 2005
16