Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.

Similar presentations


Presentation on theme: "Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed."— Presentation transcript:

1 Chapter Three: Lists, Operators, Arithmetic 1

2 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed in square brackets  The length of a list is the number of elements it has  There is a special list: the empty list [ ]

3 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail  A non-empty list can be thought of as consisting of two parts  The head  The tail  The head is the first item in the list  The tail is everything else  The tail is the list that remains when we take the first element away  The tail of a list is always a list

4 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1  [mia, vincent, jules, yolanda] Head: Tail:

5 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1  [mia, vincent, jules, yolanda] Head: mia Tail:

6 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 1  [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]

7 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2  [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:

8 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2  [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail:

9 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 2  [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

10 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3  [dead(z)] Head: Tail:

11 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3  [dead(z)] Head: dead(z) Tail:

12 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Head and Tail example 3  [dead(z)] Head: dead(z) Tail: [ ]

13 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator |  Prolog has a special built-in operator | which can be used to decompose a list into its head and tail  The | operator is a key tool for writing Prolog list manipulation predicates

14 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-

15 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-

16 © Patrick Blackburn, Johan Bos & Kristina Striegnitz The built-in operator | ?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]]. X = [ ] Y = dead(z) Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]] yes ?-

17 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variable  Suppose we are interested in the second and fourth element of a list ?- [X1,X2,X3,X4|Tail] = [mia, vincent, marsellus, jody, yolanda]. X1 = mia X2 = vincent X3 = marsellus X4 = jody Tail = [yolanda] yes ?-

18 © Patrick Blackburn, Johan Bos & Kristina Striegnitz Anonymous variables  There is a simpler way of obtaining only the information we want: ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda]. X2 = vincent X4 = jody yes ?- The underscore is the anonymous variable

19 3.2 Some operations on lists 19 1. Membership 2. Concatenation 3. Deleting an item 4. Permutations

20 3.2 Some operations on lists 20 1. Member 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]]) member([b,c], [a,[b,c]]) Definition 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. 

21 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?-

22 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]).

23 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-

24 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]).

25 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-

26 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]).

27 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-

28 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]).

29 © Patrick Blackburn, Johan Bos & Kristina Striegnitz member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]). X = yolanda; X = trudy; X = vincent; X = jules; no

30 3.2 Some operations on lists 30 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). the result of concatenation of L1 and L2 is the list L3 where L3 is the concatenation of L1 and L2 

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

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

33 3.2 Some operations on lists 33 2. Concatenation Examples How can we find the months that precede and the months that follow a given month? ?-conc(Before, [may|After], [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]

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

35 3.2 Some operations on lists 35 4. Deleting an item Example: ?- del(a,[a,b,a,a],L). L = [b,a,a]; L = [a,b,a]; no

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

37 3.2 Some operations on lists 37 5. Permutations Permutation relation has two arguments(lists), one is a permutation of the other. e.g. ?- permutation([a,b,c],P). P=[a,b,c]; P=[a,c,b]; P=[b,a,c]; …

38 3.2 Some operations on lists 38 5. Permutations 1. If the first list is empty, then the second must be also empty. permutation([],[]).


Download ppt "Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed."

Similar presentations


Ads by Google