1 / 21

Chapter Three: Lists, Operators, Arithmetic

Chapter Three: Lists, Operators, Arithmetic. Chapter three:. 3.1 Representation of lists 3.2 Some operations on lists. 3.1 Representation of lists. The list is a simple data structure.

dima
Download Presentation

Chapter Three: Lists, Operators, Arithmetic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Three: Lists, Operators, Arithmetic

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

  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: • list is empty[ ]  written as Prolog atom • list is non-empty Head: the first item Tail: the remaining items

  4. 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 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 [ ]

  5. 3.1 Representation of lists 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 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 • Membership • Concatenation • Adding an item • Deleting an item • Sublist • Permutations

  8. 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: • X is the head of L, or • 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 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: • If the first argument is empty, then the second and third arguments must be the same list conc([],L,L). • 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 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 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 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 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 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 2. Concatenation Definition of member usingconc: 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 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 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: • If X is the head. • 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 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]; L = [a,b,a]; no

  19. 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

  20. 3.2 Some operations on lists 4. Deleting an item Definition of insert: In general, the operation of insertigX 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 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): • L can be decomposed into two lists, L1 and L2 • 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).  

More Related