Upcoming SlideShare
×

# Section3 Prologppt

851 views
803 views

Published on

Published in: Sports, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
851
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
10
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Section3 Prologppt

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