SlideShare uma empresa Scribd logo
1 de 50
Week 3

PROLOG
OBJECTIVES FOR THIS SESSION

The Lists in Prolog

Tracing
SIMPLE LISTS IN PROLOG
HOW TO MAKE A LIST IN PROLOG
 pam         tom




       bob         liz




 ann         pat




       jim
HOW TO MAKE A LIST IN PROLOG
                         parent(pam, bob).
 pam         tom
                         parent(tom, bob).
                         parent(tom, liz).
                         parent(bob, pat).
                   liz   parent(pat, jim).
       bob
                         female(pam).
                         female(liz).
                         female(pat).
 ann         pat         male(tom).
                         male(bob).
                         male(jim).

       jim
HOW TO MAKE A LIST IN PROLOG
 pam         tom



       bob         liz




 ann         pat



       jim
HOW FINDALL WORKS

       findall(X,predecessor(pam,X),Preds).



                                       Output list
The query variable
EXAMPLE1

The following conversation with Prolog:
     ?- Hobbies1 = [tennis, music],
        Hobbies2 = [skiing, food],
        L = [ann, Hobbies1, tom, Hobbies2].

     Hobbies1 = [tennis, music]
     Hobbies2 = [skiing, food]
     L = [ann, [tennis, music], tom, [skiing, food] ]




                                                        8
EXAMPLE2


Treat the whole tail as a single object:
     L = [a, b, c]
     Tail = [b, c]
     L = .(a, Tail)
     L = [a | Tail]
     [a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
EXAMPLE2


Treat the whole tail as a single object:
     L = [a, b, c]
     Tail = [b, c]
     L = .(a, Tail)
     L = [a | Tail]
     [a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
MEMBERSHIP
    X is a member of L either:
       1) X is the head of L, or

       2) X is a member of the tail of L.

       member(X, L)
    Can be written in two clauses:
       member(X, [X | Tail] ).            %simple fact
       member(X, [Head | Tail] ) :-
           member(X, Tail).         %rule
    Example:
       member(b, [a,b,c] ). %true
       member(b, [a,[b,c]] ).       %false, the tail is [b,c]
       member([b,c],[a,[b,c]] ).    %true
MEMBERSHIP
Simple other tests:

findall(X,parent(X,_),Parents).
 Parents = [pam, tom, tom, bob, pat].

     Is pam a member of Parents????
  member(pam, [pam, tom,bob, pat]).
MEMBERSHIP

Parents = [pam, tom, tom, bob, pat].

  Is pam a member of HEAD of Parents????
       member(pam,[pam,Parents]).
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
HEAD AND TAIL EXAMPLE 1

   [mia, vincent, jules, yolanda]

    Head:
    Tail:
HEAD AND TAIL EXAMPLE 1

   [mia, vincent, jules, yolanda]

    Head:    mia
    Tail:
HEAD AND TAIL EXAMPLE 1

   [mia, vincent, jules, yolanda]

    Head:   mia
    Tail:   [vincent, jules, yolanda]
HEAD AND TAIL EXAMPLE 2

   [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

    Head:
    Tail:
HEAD AND TAIL EXAMPLE 2

   [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

    Head: [ ]
    Tail:
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]]]
HEAD AND TAIL EXAMPLE 3

   [dead(z)]

    Head:
    Tail:
HEAD AND TAIL EXAMPLE 3

   [dead(z)]

    Head: dead(z)
    Tail:
HEAD AND TAIL EXAMPLE 3

   [dead(z)]

    Head: dead(z)
    Tail: [ ]
THE BUILT-IN OPERATOR |


   ?- [Head|Tail] = [mia, vincent, jules, yolanda].

   Head = mia
   Tail = [vincent,jules,yolanda]
   yes

   ?-
THE BUILT-IN OPERATOR |


   ?- [X|Y] = [mia, vincent, jules, yolanda].

   X = mia
   Y = [vincent,jules,yolanda]
   yes

   ?-
THE BUILT-IN OPERATOR |


   ?- [X|Y] = [ ].

   no

   ?-
THE BUILT-IN OPERATOR |
   ?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]] .

   X=[]
   Y = dead(z)
   Z = _4543
   Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]]
   yes

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

  ?-
ANONYMOUS VARIABLES
   ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda].
   X2 = vincent
   X4 = jody
   yes

   ?-
     There is a simpler way of obtaining only
      the information we want:



   The underscore is the anonymous
    variable
THE ANONYMOUS VARIABLE

 Is used when you need to use a variable, but
  you are not interested in what Prolog
  instantiates it to
 Each occurrence of the anonymous variable
  is independent, i.e. can be bound to
  something different
MEMBER

 One of the most basic things we would like to
  know is whether something is an element of
  a list or not
 So let`s write a predicate that when given a
  term X and a list L, tells us whether or not X
  belongs to L
 This predicate is usually called member/2
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?-
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(yolanda,[yolanda,trudy,vincent,jules]).
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(yolanda,[yolanda,trudy,vincent,jules]).
  yes
  ?-
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(vincent,[yolanda,trudy,vincent,jules]).
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(vincent,[yolanda,trudy,vincent,jules]).
  yes
  ?-
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(zed,[yolanda,trudy,vincent,jules]).
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(zed,[yolanda,trudy,vincent,jules]).
  no
  ?-
MEMBER/2

  member(X,[X|T]).
  member(X,[H|T]):- member(X,T).




  ?- member(X,[yolanda,trudy,vincent,jules]).
MEMBER/2

  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
CONCATENATION OF LISTS

For concatenation we first define our rule.
Step 1: if the first list is empty
conc([],L,L).
Step2: if first list is not empty
conc(L1,L2,L3)=> conc([X|L1],L2,[X|L3]):-
                                   conc(L1,L2,L3).
[X|L1]

X          L1         L2


                 L3


    X            L3


                 L3
SAMPLE OF CONCATENATION


    conc([a],[c,d],L).
    L = [a, c, d].
LENGTH OF A LIST IN PROLOG

len([],0).                  %% not a Prolog native predicate!
len([_|L],N):-
   len(L,X),
   N is X + 1.

?- len([a,b,c,d,e,[a,x],t],X).
X=7
yes
?-
SIMPLE TRACE
SIMPLE TRACE
SIMPLE TRACE
COME OUT OF TRACE
GUI TRACER
GUI TRACER

Mais conteúdo relacionado

Destaque

How Not To Be Seen
How Not To Be SeenHow Not To Be Seen
How Not To Be SeenMark Pesce
 
Part 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative valuePart 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative valueGirija Muscut
 
Transforming the world with Information technology
Transforming the world with Information technologyTransforming the world with Information technology
Transforming the world with Information technologyGlenn Klith Andersen
 
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsPioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsWolfgang Stock
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15Niit Care
 
Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Larry Nung
 
Part2 database connection service based using vb.net
Part2 database connection service based using vb.netPart2 database connection service based using vb.net
Part2 database connection service based using vb.netGirija Muscut
 
What’s new in Visual C++
What’s new in Visual C++What’s new in Visual C++
What’s new in Visual C++Microsoft
 
Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netGirija Muscut
 
Logical Programming With ruby-prolog
Logical Programming With ruby-prologLogical Programming With ruby-prolog
Logical Programming With ruby-prologPreston Lee
 
Part 3 binding navigator vb.net
Part 3 binding navigator vb.netPart 3 binding navigator vb.net
Part 3 binding navigator vb.netGirija Muscut
 
Cognitive information science
Cognitive information scienceCognitive information science
Cognitive information scienceS. Kate Devitt
 
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...RuleML
 
Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy Eleni Zazani
 
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...GBM
 
Forensic Science Information Literacy
Forensic Science Information LiteracyForensic Science Information Literacy
Forensic Science Information LiteracyKasper Abcouwer
 

Destaque (20)

How Not To Be Seen
How Not To Be SeenHow Not To Be Seen
How Not To Be Seen
 
Presentation1
Presentation1Presentation1
Presentation1
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Part 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative valuePart 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative value
 
Transforming the world with Information technology
Transforming the world with Information technologyTransforming the world with Information technology
Transforming the world with Information technology
 
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsPioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
 
Vb.net session 15
Vb.net session 15Vb.net session 15
Vb.net session 15
 
Information Overload and Information Science / Mieczysław Muraszkiewicz
Information Overload and Information Science / Mieczysław MuraszkiewiczInformation Overload and Information Science / Mieczysław Muraszkiewicz
Information Overload and Information Science / Mieczysław Muraszkiewicz
 
Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Debugging in visual studio (basic level)
Debugging in visual studio (basic level)
 
Part2 database connection service based using vb.net
Part2 database connection service based using vb.netPart2 database connection service based using vb.net
Part2 database connection service based using vb.net
 
What’s new in Visual C++
What’s new in Visual C++What’s new in Visual C++
What’s new in Visual C++
 
Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.net
 
Logical Programming With ruby-prolog
Logical Programming With ruby-prologLogical Programming With ruby-prolog
Logical Programming With ruby-prolog
 
Part 3 binding navigator vb.net
Part 3 binding navigator vb.netPart 3 binding navigator vb.net
Part 3 binding navigator vb.net
 
Cognitive information science
Cognitive information scienceCognitive information science
Cognitive information science
 
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
 
Cpp lab 13_pres
Cpp lab 13_presCpp lab 13_pres
Cpp lab 13_pres
 
Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy
 
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
 
Forensic Science Information Literacy
Forensic Science Information LiteracyForensic Science Information Literacy
Forensic Science Information Literacy
 

Semelhante a Prolog -Cpt114 - Week3

PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG CONTENT
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologDataminingTools Inc
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG CONTENT
 
Lesson 1.1 basic ideas of sets part 1
Lesson 1.1   basic ideas of sets part 1Lesson 1.1   basic ideas of sets part 1
Lesson 1.1 basic ideas of sets part 1JohnnyBallecer
 
Set Theory
Set TheorySet Theory
Set Theoryitutor
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingYoung Alista
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingFraboni Ec
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingJames Wong
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingDavid Hoen
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingHarry Potter
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingTony Nguyen
 
BCA_Semester-I_Mathematics-I_Set theory and function
BCA_Semester-I_Mathematics-I_Set theory and functionBCA_Semester-I_Mathematics-I_Set theory and function
BCA_Semester-I_Mathematics-I_Set theory and functionRai University
 
The importance of math
The importance of mathThe importance of math
The importance of mathTayyaba Syed
 
G7 Math Q1- Week 1- Introduction of Set.pptx
G7 Math Q1- Week 1- Introduction of Set.pptxG7 Math Q1- Week 1- Introduction of Set.pptx
G7 Math Q1- Week 1- Introduction of Set.pptxJohn Loue Bigno
 
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptx
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptxQ1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptx
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptxNovyFacun1
 

Semelhante a Prolog -Cpt114 - Week3 (20)

PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In Prolog
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
Sets-newest.pptx
Sets-newest.pptxSets-newest.pptx
Sets-newest.pptx
 
Set
SetSet
Set
 
Lesson 1.1 basic ideas of sets part 1
Lesson 1.1   basic ideas of sets part 1Lesson 1.1   basic ideas of sets part 1
Lesson 1.1 basic ideas of sets part 1
 
Set Theory
Set TheorySet Theory
Set Theory
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
BCA_Semester-I_Mathematics-I_Set theory and function
BCA_Semester-I_Mathematics-I_Set theory and functionBCA_Semester-I_Mathematics-I_Set theory and function
BCA_Semester-I_Mathematics-I_Set theory and function
 
The importance of math
The importance of mathThe importance of math
The importance of math
 
Anubhav.pdf
Anubhav.pdfAnubhav.pdf
Anubhav.pdf
 
Week 4
Week 4Week 4
Week 4
 
G7 Math Q1- Week 1- Introduction of Set.pptx
G7 Math Q1- Week 1- Introduction of Set.pptxG7 Math Q1- Week 1- Introduction of Set.pptx
G7 Math Q1- Week 1- Introduction of Set.pptx
 
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptx
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptxQ1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptx
Q1 Week 1 Lesson -Concepts of Sets and Operation on Sets.pptx
 

Último

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Último (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

Prolog -Cpt114 - Week3

  • 2. OBJECTIVES FOR THIS SESSION The Lists in Prolog Tracing
  • 4. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  • 5. HOW TO MAKE A LIST IN PROLOG parent(pam, bob). pam tom parent(tom, bob). parent(tom, liz). parent(bob, pat). liz parent(pat, jim). bob female(pam). female(liz). female(pat). ann pat male(tom). male(bob). male(jim). jim
  • 6. HOW TO MAKE A LIST IN PROLOG pam tom bob liz ann pat jim
  • 7. HOW FINDALL WORKS findall(X,predecessor(pam,X),Preds). Output list The query variable
  • 8. EXAMPLE1 The following conversation with Prolog: ?- Hobbies1 = [tennis, music], Hobbies2 = [skiing, food], L = [ann, Hobbies1, tom, Hobbies2]. Hobbies1 = [tennis, music] Hobbies2 = [skiing, food] L = [ann, [tennis, music], tom, [skiing, food] ] 8
  • 9. EXAMPLE2 Treat the whole tail as a single object: L = [a, b, c] Tail = [b, c] L = .(a, Tail) L = [a | Tail] [a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
  • 10. EXAMPLE2 Treat the whole tail as a single object: L = [a, b, c] Tail = [b, c] L = .(a, Tail) L = [a | Tail] [a, b, c] = [a | [b, c] ] = [a, b | [c] ] = [a, b, c | [ ] ]
  • 11. MEMBERSHIP  X is a member of L either: 1) X is the head of L, or 2) X is a member of the tail of L. member(X, L)  Can be written in two clauses: member(X, [X | Tail] ). %simple fact member(X, [Head | Tail] ) :- member(X, Tail). %rule  Example: member(b, [a,b,c] ). %true member(b, [a,[b,c]] ). %false, the tail is [b,c] member([b,c],[a,[b,c]] ). %true
  • 12. MEMBERSHIP Simple other tests: findall(X,parent(X,_),Parents). Parents = [pam, tom, tom, bob, pat]. Is pam a member of Parents???? member(pam, [pam, tom,bob, pat]).
  • 13. MEMBERSHIP Parents = [pam, tom, tom, bob, pat]. Is pam a member of HEAD of Parents???? member(pam,[pam,Parents]).
  • 14. 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
  • 15. HEAD AND TAIL EXAMPLE 1  [mia, vincent, jules, yolanda] Head: Tail:
  • 16. HEAD AND TAIL EXAMPLE 1  [mia, vincent, jules, yolanda] Head: mia Tail:
  • 17. HEAD AND TAIL EXAMPLE 1  [mia, vincent, jules, yolanda] Head: mia Tail: [vincent, jules, yolanda]
  • 18. HEAD AND TAIL EXAMPLE 2  [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:
  • 19. HEAD AND TAIL EXAMPLE 2  [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ] Tail:
  • 20. 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]]]
  • 21. HEAD AND TAIL EXAMPLE 3  [dead(z)] Head: Tail:
  • 22. HEAD AND TAIL EXAMPLE 3  [dead(z)] Head: dead(z) Tail:
  • 23. HEAD AND TAIL EXAMPLE 3  [dead(z)] Head: dead(z) Tail: [ ]
  • 24. THE BUILT-IN OPERATOR | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-
  • 25. THE BUILT-IN OPERATOR | ?- [X|Y] = [mia, vincent, jules, yolanda]. X = mia Y = [vincent,jules,yolanda] yes ?-
  • 26. THE BUILT-IN OPERATOR | ?- [X|Y] = [ ]. no ?-
  • 27. THE BUILT-IN OPERATOR | ?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]] . X=[] Y = dead(z) Z = _4543 Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]] yes ?-
  • 28. 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 ?-
  • 29. ANONYMOUS VARIABLES ?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda]. X2 = vincent X4 = jody yes ?-  There is a simpler way of obtaining only the information we want: The underscore is the anonymous variable
  • 30. THE ANONYMOUS VARIABLE  Is used when you need to use a variable, but you are not interested in what Prolog instantiates it to  Each occurrence of the anonymous variable is independent, i.e. can be bound to something different
  • 31. MEMBER  One of the most basic things we would like to know is whether something is an element of a list or not  So let`s write a predicate that when given a term X and a list L, tells us whether or not X belongs to L  This predicate is usually called member/2
  • 32. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?-
  • 33. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]).
  • 34. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(yolanda,[yolanda,trudy,vincent,jules]). yes ?-
  • 35. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]).
  • 36. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(vincent,[yolanda,trudy,vincent,jules]). yes ?-
  • 37. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]).
  • 38. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(zed,[yolanda,trudy,vincent,jules]). no ?-
  • 39. MEMBER/2 member(X,[X|T]). member(X,[H|T]):- member(X,T). ?- member(X,[yolanda,trudy,vincent,jules]).
  • 40. MEMBER/2 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
  • 41. CONCATENATION OF LISTS For concatenation we first define our rule. Step 1: if the first list is empty conc([],L,L). Step2: if first list is not empty conc(L1,L2,L3)=> conc([X|L1],L2,[X|L3]):- conc(L1,L2,L3).
  • 42. [X|L1] X L1 L2 L3 X L3 L3
  • 43. SAMPLE OF CONCATENATION conc([a],[c,d],L). L = [a, c, d].
  • 44. LENGTH OF A LIST IN PROLOG len([],0). %% not a Prolog native predicate! len([_|L],N):- len(L,X), N is X + 1. ?- len([a,b,c,d,e,[a,x],t],X). X=7 yes ?-
  • 48. COME OUT OF TRACE