Chapter Three Lists Operators Arithmetic 1 Chapter three

  • Slides: 21
Download presentation
Chapter Three: Lists, Operators, Arithmetic 1

Chapter Three: Lists, Operators, Arithmetic 1

Chapter three: 3. 1 3. 2 2 Representation of lists Some operations on lists

Chapter three: 3. 1 3. 2 2 Representation of lists Some operations on lists

3. 1 Representation of lists � The list is a simple data structure. �

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, skiing written 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: list is empty[ ] written as Prolog atom Head: the first item 2. list is non-empty 1. Tail: the remaining items 3

3. 1 Representation of lists [ann, tennis, tom, skiing] The head is ann The

3. 1 Representation of lists [ann, tennis, tom, skiing] The head is ann The tail is the list [tennis, tom, skiing] The head can be any Prolog object The tail has to be list ann 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, [])))) 4 tennis tom skiing [ ]

3. 1 Representation of lists Example ? - List 1=[a, b, c], List 2=.

3. 1 Representation of lists Example ? - List 1=[a, b, c], List 2=. (a, b, c). List 1= [a, b, c] List 2=[a, b, c] ? - Hobbies 1=. (tennis, . (reading, [])), Hobbies 2=[skiing, food], L=[ann, Hobbies 1, tom, Hobbies 2]. Hobbies 1=[tennis, reading] Hobbies 2=[skiing, food] L=[ann, [tennis, reading], tom, [skiing, food]] 5

3. 1 Representation of lists The element of a list can be object of

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 6

3. 2 Some operations on lists 1. 2. 3. 4. 5. 6. 7 Membership

3. 2 Some operations on lists 1. 2. 3. 4. 5. 6. 7 Membership Concatenation Adding an item Deleting an item Sublist Permutations

3. 2 Some operations on lists 1. Membership member(X, L) where X is an

3. 2 Some operations on lists 1. Membership member(X, L) where X is an object and L is a list member(X, L) is true if X occurs in L � e. 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). 8

3. 2 Some operations on lists 2. Concatenation conc(L 1, L 2, L 3)

3. 2 Some operations on lists 2. Concatenation conc(L 1, L 2, L 3) here L 1 and L 2 are two lists and L 3 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|L 1] the result of concatenation of L 1 and L 2 is the list [X|L 3] where L 3 is the concatenation of L 1 and L 2 conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3). 9

3. 2 Some operations on lists 2. Concatenation Examples ? - conc([a, b, c],

3. 2 Some operations on lists 2. Concatenation Examples ? - 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] 10

3. 2 Some operations on lists 2. Concatenation Examples ? - conc(L 1, L

3. 2 Some operations on lists 2. Concatenation Examples ? - conc(L 1, L 2, [a, b, c]) L 1=[] L 2=[a, b, c]; L 1=[a] L 2=[b, c]; L 1=[a, b] L 2=[c]; L 1=[a, b, c] L 2=[]; no 11 (decompose)

3. 2 Some operations on lists 2. Concatenation Examples How can we find the

3. 2 Some operations on lists 2. Concatenation Examples How 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]). 12

3. 2 Some operations on lists 2. Concatenation Examples How can we find immediate

3. 2 Some operations on lists 2. Concatenation Examples How can we find immediate predecessor and the immediate successor of May? ? -conc(_, [M 1, may, M 2|_], [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]). Month 1 = apr Month 2 = jun 13

3. 2 Some operations on lists 2. Concatenation Examples How can we delete from

3. 2 Some operations on lists 2. Concatenation Examples How can we delete from some list L 1, everything that follows three successive occurrence of z in L 1 together with the three z’s L 1=[a, b, z, z, c, z, z, z, d, e], conc(L 2, [z, z, z|_], L 1). ? - L 1=[a, b, z, z, c, z, z, z, d, e] L 2=[a, b, z, z, c] 14

3. 2 Some operations on lists 2. Concatenation Definition of member using conc: We

3. 2 Some operations on lists 2. Concatenation Definition of member using conc: We can program the membership relation using conc: member 1(X, L): - conc(L 1, [X|L 2], 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. member 1(X, L): - conc(_, [X|_], L). member(X, [X|Tail]). member(X, [Head|Tail]): - member(X, Tail). Example ? - member 1( b, [a, b, c]). True 15

3. 2 Some operations on lists 3. Adding an item Simply to put an

3. 2 Some operations on lists 3. Adding an item Simply 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(X, L, [X|L]). Example ? -add(z, [a, b, c], New. List). ? - NEWLIST = [z, [a, b, c]]. 16 add( z, [a, b, c], New. List). NEWLIST = [z, a, b, c].

3. 2 Some operations on lists 4. Deleting an item Deleting item X from

3. 2 Some operations on lists 4. Deleting an item Deleting item X from a list L can be programmed as a relation: del(X, L, L 1) where L 1 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, Tail 1]): - del(X, Tail 1). 17

3. 2 Some operations on lists 4. Deleting an item Each execution will only

3. 2 Some operations on lists 4. Deleting an item Each 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]; no 18

3. 2 Some operations on lists 4. Deleting an item del can be used

3. 2 Some operations on lists 4. Deleting an item del 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 19

3. 2 Some operations on lists 4. Deleting an item Definition of insert: In

3. 2 Some operations on lists 4. Deleting an item Definition of insert: In general, the operation of insertig X at any place in some list List giving Bigger. List can be defined by the clause: insert(X, List, Bigger. List): 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 List member 2(X, List): - del(X, List, _). 20

3. 2 Some operations on lists 5. Sublist relation has two arguments, a list

3. 2 Some operations on lists 5. Sublist relation has two arguments, a list L and a list S such that S occurs within L as its sublist e. 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 member 1 (more general): 1. L can be decomposed into two lists, L 1 and L 2 2. L 2 can be decomposed into two lists, S and some L 3. sublist(S, L): - conc(L 1, L 2, L), conc(S, L 3, L 2). conc([], L, L). conc([X|L 1], L 2, [X|L 3]): - conc(L 1, L 2, L 3). 21