section3 prologppt
TRANSCRIPT
Chapter 3
Lists , Operators and Arithmetic
Lists are one of the fundamental data structures in A.I.
Lists provide an ordered collection of other data objects.
The great advantages is that list’s ability to grow and shrink during execution.
A list is a simple data structure for collecting data objects together.
Elements in lists can be numbers , atoms , lists or anything else.
We write lists within square brackets [] with items separated by commas.
Lists are one of the fundamental data structures in A.I.
Lists provide an ordered collection of other data objects.
The great advantages is that list’s ability to grow and shrink during execution.
A list is a simple data structure for collecting data objects together.
Elements in lists can be numbers , atoms , lists or anything else.
We write lists within square brackets [] with items separated by commas.
Lists
Empty Non empty
[] no space.
Ex.1:[ann , tennis , tom , football].
Ex.2:[ann , X , sport(tennis) , [tom , skiing]]. ann: atom X: variable sport(tennis):structure with functor sport / 1 argument. [tom , skiing]:anther list.
[ann , tennis , tom , football]
Functor
i.e. :to represent it as a structure with functor . , and in binary tree (2 - arguments).
We can represent list as a structure using functor ./2
Where: Head: First argument object. Tail: The rest of objects.
Head Tail
Two arguments
Ex.: [ann , tennis , tom , football].
.
ann .
tennis .
tom .
football []
. (ann , . (tennis , . (tom , . (football , [])))).
Note: the tail has to be a list
Some operations that are useful in lists:(1)Membership.(2)Concatenation.(3)Add item.(4)Delete item(5)Sub list(6)Permutation.
[a , b | [c , d]] , [a , b , c | [d]] , [a , b , c , d | []] Ex.: [[a , b] , c] = [[a , b] | [c]]
Ex.: [a , b , c , d] = [a | [b , c , d]]
(1) Membership / 2
Ex.: member(a , [a , b , c])
objectobject
Ex.: member (X , L)
We use it to check whether some object is element of a list or not.
Means: number of arguments are two
list list
Yes Yes
objectobject list list
member(a , [1 , b , c])
No No
when X is a member in list?
let us define the rule:(1)member (X , [X | _ ]).(2) member (X , [Head | Tail]) :- member (X , Tail).
X is a member of list L if either:(1) X is a head of L.(2) X is a member of tail.
Question: ?- member (X , [a , b , c]).
(Fact)
X = a; X = b; X = c; no
How prolog answer this question?!!
Question: ?- member (a , [b , c , a , d]).
Let us see:(1)Member (X , [X | _ ]). dose not satisfy.(2)So , it will leave it and go to other member relations , it found member (X , [Head | Tail]):- member (X , Tail).Try to satisfy (subgoal)Here : Head = b Tail = [c , a , d]
member (X , Tail)
To satisfy member (X , [c , a , d])It go to member (X , [X | _ ]) again and try to satisfy , but it is not satisfied ,then go to member(X , Tail) again.But here Head = [b , c] Tail = [a , d]
To satisfy member (X , [a , d])It go to member(X , [X | _ ]) again and try to satisfy,but here at this time it is satisfied then it answers YesYes
Stop Practice
Question: ?- conc ([1 , 2] , [a , b] , [1 , 2 , b , a]). prolog answers : No Not in the same order. L1 L2 L3 [] L L [X | L1] L2 [X | L3]
Two listsTwo lists
Means combining and interference.Concatenation of two lists obtain a third list. conc (L1 , L2 , L3)
Concatenation /3Concatenation /3 Means : three
lists
Conc of L1 , L2.Conc of L1 , L2.
Why??Why??The sameThe same
[Head | Tail][Head | Tail]
emptyempty
X L1 X L1 X L3 X L3 L2L2
Two rules:
Two rules:
Using (trace.) order we found that it is important to use this command to explain what prolog does to answer a question by all steps.
Answer: practical Let us see
Question: ?- conc (L1 , L2 , [a , b , c]).
conc ([] , L , L). conc ([X | L1] , L2 , [X | L3]) :- conc (L1 , L2 , L3).
Answer:L1=[]L2 =[a , b , c];L1 =[a] L2 =[b , c];
Answer:L1=[]L2 =[a , b , c];L1 =[a] L2 =[b , c];
L1 =[a , b]L2 =[c];L1 =[a , b ,c]L2 =[];no
L1 =[a , b]L2 =[c];L1 =[a , b ,c]L2 =[];no
Decomposition process.For a list into two lists
Example:Find the months that precede and the months that follow a given month?Assume that the given month is may.
Vip:Vip:
Answer: ?- conc(Before , [may | After] , [ jan , feb , mar , apr , may , jun , jul , aug , sep , oct , nov , dec]).Prolog answers: Before =[ jan , feb , mar , apr]. After =[ jun , jul , aug , sep , oct , nov , dec].
Write in prolog(1)The month just after and the month just
before may.(2)Define membership relation using conc
relation.(3)Define the relation last (Item , List)So that Item is the last element of the list List.Write two versions (a) using the conc relation (b) without conc
relation.