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

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Advertisements

Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Chapter Three: Lists, Operators, Arithmetic CS 461.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
Line Efficiency     Percentage Month Today’s Date
CS Data Structures Chapter 10 Search Structures (Selected Topics)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
LING 388: Language and Computers Sandiway Fong Lecture 4.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Artificial Intelligence CS370D Prolog programming List operations.
Jan 2016 Solar Lunar Data.

Q1 Jan Feb Mar ENTER TEXT HERE Notes
Jul Aug Sept Oct Nov Dec Jan Feb Mar Apr May Jun
Lists Prolog Pepper Credit to : Blackburn, Patrick, Johan Bos and Kristina Striegnitz. Learn Prolog Now. London: College Publications, (ISBN )

Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall

Mammoth Caves National Park, Kentucky
2017 Jan Sun Mon Tue Wed Thu Fri Sat
Timeline PowerPoint Template

2013 VFC PRESIDENT’S REPORT
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Jul Aug Sept Oct Nov Dec Jan Feb Mar Apr May Jun
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com


Step 3 Step 2 Step 1 Put your text here Put your text here
Calendar Year 2009 Insure Oklahoma Total & Projected Enrollment
MONTH CYCLE BEGINS CYCLE ENDS DUE TO FINANCE JUL /2/2015
Jan Sun Mon Tue Wed Thu Fri Sat
Electricity Cost and Use – FY 2016 and FY 2017
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com

Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

Chapter Three: Lists, Operators, Arithmetic 1

© 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 [ ]

© 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

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

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

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

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

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

© 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]]]

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

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

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

© 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

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

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

© 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 ?-

© 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 ?-

© 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

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

3.2 Some operations on lists 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. 

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

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

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

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

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

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

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

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

© 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

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

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

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

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

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

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

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

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

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