Download presentation

Presentation is loading. Please wait.

Published byKhalil Paul Modified over 2 years ago

1
Chapter Three: Lists, Operators, Arithmetic 1

2
Chapter three: 3.1Representation of lists 3.2Some operations on lists 2

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

4
3.1 Representation of lists 4 [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 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

5
3.1 Representation of lists 5 Example ?- 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]]

6
3.1 Representation of lists 6 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

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

8
3.2 Some operations on lists 8 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]]) Defini member([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).

9
3.2 Some operations on lists 9 2. Concatenation conc(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 L2 conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3).

10
3.2 Some operations on lists 10 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]

11
3.2 Some operations on lists 11 2. Concatenation Examples ?- 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

12
3.2 Some operations on lists 12 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]).

13
3.2 Some operations on lists 13 2. Concatenation Examples How 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 = apr Month2 = jun

14
3.2 Some operations on lists 14 2. Concatenation Examples How 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]

15
3.2 Some operations on lists 15 2. Concatenation Definition 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

16
3.2 Some operations on lists 16 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(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].

17
3.2 Some operations on lists 17 4. Deleting an item Deleting 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).

18
3.2 Some operations on lists 18 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

19
3.2 Some operations on lists 19 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

20
3.2 Some operations on lists 20 4. Deleting an item Definition 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 List member2(X,List):- del(X,List,_).

21
3.2 Some operations on lists 21 5. Sublist 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 member1 (more general): 1. L can be decomposed into two lists, L1 and L2 2. 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).

Similar presentations

OK

1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.

1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on indian armed forces Ppt on electron beam machining Ppt on lower bound theory Ppt on schottky diode characteristics Ppt on cold chain storage of food Ppt on topic health and medicine Ppt on political parties and electoral process in canada Ppt on small and large scale industries in india Ppt on cross docking distribution Ppt on cross docking operations