chapter three: lists, operators, arithmetic 1. chapter three: 3.1representation of lists 3.2some...

21
Chapter Three: Lists, Operators, Arithmetic 1

Upload: khalil-paul

Post on 14-Dec-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

1

Chapter Three: Lists, Operators, Arithmetic

Page 2: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

2

Chapter three:3.1 Representation of lists3.2 Some operations on lists

Page 3: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

3

3.1 Representation of lists The list is a simple data structure. A list is a sequence of any number of items.

e.g. ann, tennis, tom, skiingwritten in prolog as: [ann, tennis, tom, skiing]

We have learned that: all structured objects in Prolog are trees.How can a list be represented as a standard Prolog object?There are 2 cases:1. list is empty[ ] written as Prolog atom2. list is non-empty Head: the first item

Tail: the remaining items

Page 4: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

4

3.1 Representation of lists[ann, tennis, tom, skiing]

The head is annThe tail is the list [tennis, tom, skiing]

The head can be any Prolog objectThe tail has to be list

The head and the tail are combined into a structure by a special functor ‘ . ’ . (Head, Tail)

The tail is a list, it is either empty or it has its own head and tail.

The list above can be represented as the term:

.(ann, .(tennis, .(tom, .(skiing, []))))

ann

tennis

tom

[ ] skiing

Page 5: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

5

3.1 Representation of listsExample?- List1=[a,b,c],

List2=.(a,b,c).

List1= [a,b,c]List2=[a,b,c]

?- Hobbies1= .(tennis,.(reading,[])),

Hobbies2=[skiing,food],

L=[ann,Hobbies1,tom,Hobbies2].

Hobbies1=[tennis,reading]Hobbies2=[skiing,food]L=[ann,[tennis,reading],tom,[skiing,food]]

Page 6: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

6

3.1 Representation of lists

The element of a list can be object of any kind; it can be also list e.g.

L= [a,b,c] Tail=[b,c] and L=.(a, Tail) L=[a|Tail]

Alternative ways of writing the list L are: [a,b,c]=[a|[b,c]]=[a,b|[c]]=[a,b,c|[]]

alternative notation to express lists, Vertical bar that separate s the head and the tail

Page 7: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

7

3.2 Some operations on lists1. Membership2. Concatenation3. Adding an item4. Deleting an item5. Sublist6. Permutations

Page 8: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

8

3.2 Some operations on lists1. Membershipmember(X,L) where X is an object and L is a listmember(X,L) is true if X occurs in Le.g. member(b, [a,b,c]) member(b, [a,[b,c]])

Definimember([b,c], [a,[b,c]])tion of member:X is a member of L if either:(1) X is the head of L, or(2) X is a member of the tail of L.member(X,[X|Tail]).

member(X,[Head|Tail]):- member(X,Tail).

Page 9: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

9

3.2 Some operations on lists2. Concatenationconc(L1,L2,L3) here L1 and L2 are two lists and L3 is

their concatenation.conc([a,b],[c,d],[a,b,c,d])

conc([a,b],[c,d],[a,b,a,c,d])

Definition of conc:(1) If the first argument is empty, then the second and

third arguments must be the same list conc([],L,L).(2) If the first argument of conc is non-empty list, then it

has a head and tail and must look like this [X|L1]the result of concatenation of L1 and L2 is the list [X|L3] where L3 is the concatenation of L1 and L2conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3).

Page 10: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

10

3.2 Some operations on lists2. ConcatenationExamples?- conc([a,b,c],[1,2,3],L)

L=[a,b,c,1,2,3]

?-conc([a,[b,c],d],[a,[],b],L)

L=[a,[b,c],d,a,[],b]

Page 11: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

11

3.2 Some operations on lists2. ConcatenationExamples?- conc(L1,L2,[a,b,c]) (decompose)L1=[]

L2=[a,b,c];

L1=[a]

L2=[b,c];

L1=[a,b]

L2=[c];

L1=[a,b,c]

L2=[];

no

Page 12: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

12

3.2 Some operations on lists2. ConcatenationExamplesHow can we find the months that precede and the months that follow a given month??-conc(A, [may|B],

[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).

Before = [jan, feb, mar, apr]After = [jun, jul, aug, sep, oct, nov, dec]

?-conc(A, B,[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).

Page 13: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

13

3.2 Some operations on lists2. ConcatenationExamplesHow can we find immediate predecessor and the immediate successor of May??-conc(_,[M1,may,M2|_],

[jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]).

Month1 = aprMonth2 = jun

Page 14: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

14

3.2 Some operations on lists2. ConcatenationExamplesHow can we delete from some list L1, everything that follows three successive occurrence of z in L1 together with the three z’s

?-L1=[a,b,z,z,c,z,z,z,d,e],conc(L2,[z,z,z|_],L1).L1=[a,b,z,z,c,z,z,z,d,e]L2=[a,b,z,z,c]

Page 15: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

15

3.2 Some operations on lists2. ConcatenationDefinition of member using conc:We can program the membership relation using conc:member1(X,L):- conc(L1,[X|L2],L).

X is a member of list L if L can be decopmosed into two lists so that the second one has X as its head.member1(X,L):- conc(_,[X|_],L).

member(X,[X|Tail]).

member(X,[Head|Tail]):- member(X,Tail).

Example

?- member1( b,[a,b,c]).

True

Page 16: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

16

3.2 Some operations on lists3. Adding an itemSimply to put an item in front of a list:[X|L]

Definition of add:

The procedure can be written explicitly as the fact:

add(X,L,[X,L]).Example

?-add(z,[a,b,c],NewList).

NEWLIST = [z,[a,b,c]].

add(X,L,[X|L]).Example?- add( z,[a,b,c],NewList).

NEWLIST = [z,a,b,c].

Page 17: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

17

3.2 Some operations on lists4. Deleting an itemDeleting item X from a list L can be programmed as a relation:del(X,L,L1)

where L1 is equal to the list L with the item X removed.

Definition of del:It can be defined similarly to the membership relation, again we have 2 cases:1. If X is the head.2. If X is in the tail.del(X,[X|Tail],Tail).

del(X,[Y|Tail],[Y,Tail1]):- del(X,Tail,Tail1).

Page 18: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

18

3.2 Some operations on lists4. Deleting an itemEach execution will only delete one occurrence of X, leaving the other untouched.Example:?- del(a,[a,b,a,a],L).

L = [b,a,a];L = [a,b,a];L = [a,b,a];no

Page 19: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

19

3.2 Some operations on lists4. Deleting an itemdel can be used in the inverse direction, to add an item

to a list by inserting the new item anywhere in the list.Example:If we want to insert ‘a’ in the list [1,2,3] then we can do that by asking the question: What is L such that after deleting ‘a’ from L we obtain

[1,2,3]??- del(a,L,[1,2,3]).L = [a,1,2,3];

L = [1,a,2,3];

L = [1,2,a,3];

L = [1,2, 3,a];

no

Page 20: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

20

3.2 Some operations on lists4. Deleting an itemDefinition of insert:In general, the operation of insertig X at any place

in some list List giving BiggerList can be defined by the clause:

insert(X,List,BiggerList):- del(X,Biggerlist,List).

Definition of member using del:Also, we can define membership relation using del:The idea is: some X is a member of a List if X can

be deleted from Listmember2(X,List):- del(X,List,_).

Page 21: Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

21

3.2 Some operations on lists5. SublistSublist relation has two arguments, a list L and a list S such that S occurs within L as its subliste.g. sublist([c,d,e],[a,b,c,d,e,f])

sublist([c,e],[a,b,c,d,e,f])

Definition of sublist:Based on the same idea as member1 (more general):1. L can be decomposed into two lists, L1 and L22. L2 can be decomposed into two lists, S and some L3.sublist(S,L):- conc(L1,L2,L),

conc(S,L3,L2).

conc([],L,L).conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3).