Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.

Slides:



Advertisements
Similar presentations
Part 1 The Prolog Language Chapter 3 Lists, Operators, Arithmetic
Advertisements

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.
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Chapter Three: Lists, Operators, Arithmetic CS 461.
زبان برنامه نویسی پرولوگ
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Line Efficiency     Percentage Month Today’s Date
Unit Number Oct 2011 Nov 2011 Dec 2011 Jan 2012 Feb 2012 Mar 2012 Apr 2012 May 2012 Jun 2012 Jul 2012 Aug 2012 Sep (3/4 Unit) 7 8 Units.
CS Data Structures Chapter 10 Search Structures (Selected Topics)
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Chapter 2 Section 2.1 Sets and Set Operations. A set is a particular type of mathematical idea that is used to categorize or group different collections.
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),
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Gantt Chart of Progress FY
Jan 2016 Solar Lunar Data.
<workgroup name>

Q1 Jan Feb Mar ENTER TEXT HERE Notes
Jul Aug Sept Oct Nov Dec Jan Feb Mar Apr May Jun

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
Gantt Chart of Progress FY
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
Bourke properties Houston, Whitney relocation info 23/02/2019.

©G Dear 2008 – Not to be sold/Free to use
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ITEM 1 ITEM 2 ITEM 3
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
Pilot of revised survey
Presentation transcript:

Chapter Three: Lists, Operators, Arithmetic 1

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

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

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

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

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

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

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). 

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). 

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(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]).

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

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

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

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

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

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

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

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