CS370D Prolog programming List operations Outline List operations Membership Appending Reversing Select predicate Deleting an item from list Length of a list ID: 526056
Download Presentation The PPT/PDF document "1 Artificial Intelligence" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
1
Artificial Intelligence CS370D
Prolog programming
List operations.Slide2
Outline:
List operations: - Membership.
-
Appending.
- Reversing. - Select predicate. - Deleting an item from list. - Length of a list.
2Slide3
3
Membership operation:
A common predicate when manipulating lists is a membership test:
Is a given value a member of a list?
The member built-in predicate takes two arguments. If the first argument is any term except a variable and the second argument is a list.If the first argument is an variable, it is bound to an element of the list working to find all the elements of a list in turn from left to right.
member succeeds if the
first argument is a member of the list denoted by the second argument (i.e. one of its list elements).Slide4
4
Membership operation examples:
Examples:
?-member(maha, [
laila,maha,sara
]).
true
?- member(dog, [cat,[
dog,horse
]]).
false
?- member([
apple,orange
], [banana,[
apple,orange
]]).
true
?- member(x,[]).
false
?- member([1,2,3],[
a,b
,[1,2,3],c]).
true
?- member(X,[
a,b,c
]).
X =a ;X = b ;X = c.Slide5
5
Append operation:
The append built-in predicate takes three arguments:
append(L1,L2,L3). - If the first two arguments L1,L2 are lists and the third argument is an variable,
the third argument is bound to a list comprising the first two lists concatenated.
- If the all arguments are lists
, then the predicate check if the last argument
L3
contains all element of
L1 and L2
and then return true or false
.
- If the
first two arguments
are
variables and
the last argument is a list
then try to divide the list between these variables. Slide6
6
Append operation examples:
Examples
: ?- append([1,2,3,4],[5,6,7,8,9],L). L = [1,2,3,4,5,6,7,8,9]
?- append([],[1,2,3],L).
L = [1,2,3]
?-
append([[a,b,c],d,e,f],[g,h,[i,j,k]],L).
L = [[a,b,c],d,e,f,g,h,[i,j,k]]Slide7
7
Append operation examples: (cont)
Examples
: ?- append([a,b
],[
c,d
],[
a,b,c,d
]).
yes
?- append([
a,b
],[
c,d
],[
a,b,a,c,d
]).
no
?- append([],[
b,c
],[
b,c,d
]).
no
?-append([a,[
b,c
],d],[
a,b
],L).
L = [a, [b, c], d,
a,b
].Slide8
8
Append operation examples: (cont)
Examples
:?- append(L1,L2,[1,2,3]).
L1 = [],L2 = [1,2,3] ;
default
L1 = [1],L2 = [2,3] ;
L1 = [1,2],L2 = [3] ;
L1 = [1,2,3],L2 = [] .
?- L = [1,2,3,4,5], append(L1,[_,_,_],L).
L = [1, 2, 3, 4, 5],
L1 = [1, 2]Slide9
9
Reversing
a
list
:The term reverse the elements in L1 and put them reversed in L2 variable:
reverse(L1,L2).
If all arguments are
lists
then
check if the second
argument
reversed from the first then return true of
false.
reverse(List1,List2).Slide10
10
Reversing
a
list
:Examples:
?- reverse([1,2,3,4], What).
What=[4,3,2,1]
?- reverse([
a,b,c
],[
c,b,a
]).
trueSlide11
11
Select
predicate
:
This predicate check if L2 has the same items in L1 after element removed as the same order.
select(element,L1,L2).
Examples:
?- select(a,[
a,c
],[
a,d
]).
false.
?- select(1,[1,2,3],[3,2]).
false
. Not the same order.
?- select(a,[
a,b,c
],[
b,c
]).
true .
?- select(a,[
b,c
],[
b,c
]).
false
. The first list does not contain the item.Slide12
12
Delete
item
from
list:The term delete the element fromL1 where L2 is equal to L1 with the item X removed
delete(L1, element,L2).
Examples:
?- delete([
a,b,c
],
a,L
).
L = [b, c].
?- delete([a,b,c],g,H
).
H = [a, b, c].
?- delete([
a,b,a,a
],
a,L
).
L = [b].Slide13
13
Length of a list:
length (List, N)
This term used to count the elements in a list List and put the numbers in the N variable
.
Examples:
?- length([
a,b,c
],N).
N = 3
?- length([],M).
M = 0.
?- length([
a,b,c
],5).
False.Slide14
14
Reference
:
More predicates of SWI prolog lists can found here:
http://www.swi-prolog.org/pldoc/man?section=lists