SlideShare a Scribd company logo
1 of 13
CSE240 – Introduction to
Programming Languages
Lecture 25:
Programming with Prolog IV
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Announcement
• Homework 6 due Nov 29.
• Quiz 9 due Nov 29.
Expressions and Recursion
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Arithmetic Queries
?- X is 3 + 5.
X=8
?- X is 3 mod 2.
X=1
?- X is 3 mod 2 - 1 * 5 / 9.
X=0.4444444444444444
?- X is 5+4, Y is 5-4.
X=9
Y=1
?- X is 5*4, Y is 2^3.
X=20
Y=8
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Arithmetic Queries
?- X is 234556^100.
X = 1058411813164889882981775915184...
?- X is 5/3.
X=1.66666667
?- X is sqrt(3),Y is 3^0.5.
X=Y
Y=1.7320508075688772
?- X is 8 mod 3.
X=2
?- Y is 10^3 * (1+1) + 3.
Y=2003
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Question
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Test Yourselves
• X is 5 /3, X = 1.
false
• X is 5 /3; X = 1.
X = 1.6666666667
X = 1
• doSomething(A,B,P) :- P is A + B.
?-doSomething(3,2,P).
?-doSomething(3,2,10).
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Recursion
% this is fact
factorial(0, 1).
% this is a rule
% the factorial of F is N*F1
% if N>0 and
% N1 is N-1 and
% the factorial of N1 is F1
factorial(N, F) :-
N>0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
?- factorial (3, W).
W=6
1. factorial(3, W)
apply rule 2
2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1
solve the individual parts
true, 2 is 3-1, factorial (2, F1), F is 3*F1
apply rule 2 again
3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2
solve the individual parts
true, 1 is 2-1, factorial (1, F1), F1 is 2*F2
apply rule 2 again
4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3
solve the individual parts
true, 0 is 1-1, factorial (0, F3), F2 is 1*F3
apply rule 1
5. factorial (0, 1)
F3 is 1
6. Go back, replace F3 to get F2, then F2 to get F1,
then F1 to get F.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Recursion
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Test Yourselves
fib(0, 0).
fib(1, 1).
fib(X, Y) :- X > 1,
X2 is X – 2, fib(X2, Y2),
X1 is X – 1, fib(X1, Y1),
Y is Y1 + Y2.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Fibonacci
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Example 1
Facts
• mother_child(trude, sally).
• father_child(tom, sally).
• father_child(tom, erica).
• father_child(mike, tom).
Rules
• sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
• parent_child(X, Y) :- father_child(X, Y).
• parent_child(X, Y) :- mother_child(X, Y).
Query
• ?- sibling(sally, erica).
Yes

More Related Content

What's hot

Subtract integers
Subtract integersSubtract integers
Subtract integersjfincel32
 
Associative Property
Associative PropertyAssociative Property
Associative Propertyhiratufail
 
2.2 add integers ws day 2
2.2 add integers   ws day 22.2 add integers   ws day 2
2.2 add integers ws day 2bweldon
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2bweldon
 
Quiz review and start of lesson 6
Quiz review and start of lesson 6Quiz review and start of lesson 6
Quiz review and start of lesson 6Erik Tjersland
 
Subtracting integers
Subtracting integersSubtracting integers
Subtracting integersrinabells
 
2.5 dividing integers ws day 1
2.5 dividing integers ws day 12.5 dividing integers ws day 1
2.5 dividing integers ws day 1bweldon
 
Quiz Bowl Review For Interim Ii Accelerated
Quiz Bowl Review For Interim Ii AcceleratedQuiz Bowl Review For Interim Ii Accelerated
Quiz Bowl Review For Interim Ii Acceleratedmmeddin
 
Fractions divide
Fractions divideFractions divide
Fractions divideLy Shan
 
Y1T2- IM Math ACE
Y1T2- IM Math ACEY1T2- IM Math ACE
Y1T2- IM Math ACEKeefe Ng
 
Solving One Step Equations
Solving One Step EquationsSolving One Step Equations
Solving One Step EquationsRenegarmath
 
A2 Test 2 study guide with answers (revised)
A2 Test 2 study guide with answers (revised)A2 Test 2 study guide with answers (revised)
A2 Test 2 study guide with answers (revised)vhiggins1
 
A2 Test 2 study guide with answers
A2 Test 2 study guide with answersA2 Test 2 study guide with answers
A2 Test 2 study guide with answersvhiggins1
 
3 lesson 3 problems involving maximum and minimum points
3 lesson 3 problems involving maximum and minimum points3 lesson 3 problems involving maximum and minimum points
3 lesson 3 problems involving maximum and minimum pointsMelchor Cachuela
 
Day 4 evaluating with add and subtract
Day 4 evaluating with add and subtractDay 4 evaluating with add and subtract
Day 4 evaluating with add and subtractErik Tjersland
 

What's hot (20)

Subtract integers
Subtract integersSubtract integers
Subtract integers
 
Addition of Integers
Addition of IntegersAddition of Integers
Addition of Integers
 
Associative Property
Associative PropertyAssociative Property
Associative Property
 
2.2 add integers ws day 2
2.2 add integers   ws day 22.2 add integers   ws day 2
2.2 add integers ws day 2
 
4-Cm11 09-10
4-Cm11 09-104-Cm11 09-10
4-Cm11 09-10
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2
 
Quiz review and start of lesson 6
Quiz review and start of lesson 6Quiz review and start of lesson 6
Quiz review and start of lesson 6
 
Subtracting integers
Subtracting integersSubtracting integers
Subtracting integers
 
2.5 dividing integers ws day 1
2.5 dividing integers ws day 12.5 dividing integers ws day 1
2.5 dividing integers ws day 1
 
Quiz Bowl Review For Interim Ii Accelerated
Quiz Bowl Review For Interim Ii AcceleratedQuiz Bowl Review For Interim Ii Accelerated
Quiz Bowl Review For Interim Ii Accelerated
 
Fractions divide
Fractions divideFractions divide
Fractions divide
 
Day 3 finding square roots
Day 3 finding square rootsDay 3 finding square roots
Day 3 finding square roots
 
Y1T2- IM Math ACE
Y1T2- IM Math ACEY1T2- IM Math ACE
Y1T2- IM Math ACE
 
Summation Notation
Summation NotationSummation Notation
Summation Notation
 
Solving One Step Equations
Solving One Step EquationsSolving One Step Equations
Solving One Step Equations
 
A2 Test 2 study guide with answers (revised)
A2 Test 2 study guide with answers (revised)A2 Test 2 study guide with answers (revised)
A2 Test 2 study guide with answers (revised)
 
A2 Test 2 study guide with answers
A2 Test 2 study guide with answersA2 Test 2 study guide with answers
A2 Test 2 study guide with answers
 
3 lesson 3 problems involving maximum and minimum points
3 lesson 3 problems involving maximum and minimum points3 lesson 3 problems involving maximum and minimum points
3 lesson 3 problems involving maximum and minimum points
 
4-Cm11 09-10
4-Cm11 09-104-Cm11 09-10
4-Cm11 09-10
 
Day 4 evaluating with add and subtract
Day 4 evaluating with add and subtractDay 4 evaluating with add and subtract
Day 4 evaluating with add and subtract
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 

Recently uploaded

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 

201801 CSE240 Lecture 25

  • 1. CSE240 – Introduction to Programming Languages Lecture 25: Programming with Prolog IV Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Announcement • Homework 6 due Nov 29. • Quiz 9 due Nov 29.
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Arithmetic Queries ?- X is 3 + 5. X=8 ?- X is 3 mod 2. X=1 ?- X is 3 mod 2 - 1 * 5 / 9. X=0.4444444444444444 ?- X is 5+4, Y is 5-4. X=9 Y=1 ?- X is 5*4, Y is 2^3. X=20 Y=8
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Arithmetic Queries ?- X is 234556^100. X = 1058411813164889882981775915184... ?- X is 5/3. X=1.66666667 ?- X is sqrt(3),Y is 3^0.5. X=Y Y=1.7320508075688772 ?- X is 8 mod 3. X=2 ?- Y is 10^3 * (1+1) + 3. Y=2003
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Question
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 Test Yourselves • X is 5 /3, X = 1. false • X is 5 /3; X = 1. X = 1.6666666667 X = 1 • doSomething(A,B,P) :- P is A + B. ?-doSomething(3,2,P). ?-doSomething(3,2,10).
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 Recursion % this is fact factorial(0, 1). % this is a rule % the factorial of F is N*F1 % if N>0 and % N1 is N-1 and % the factorial of N1 is F1 factorial(N, F) :- N>0, N1 is N - 1, factorial(N1, F1), F is N * F1. ?- factorial (3, W). W=6 1. factorial(3, W) apply rule 2 2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1 solve the individual parts true, 2 is 3-1, factorial (2, F1), F is 3*F1 apply rule 2 again 3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2 solve the individual parts true, 1 is 2-1, factorial (1, F1), F1 is 2*F2 apply rule 2 again 4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3 solve the individual parts true, 0 is 1-1, factorial (0, F3), F2 is 1*F3 apply rule 1 5. factorial (0, 1) F3 is 1 6. Go back, replace F3 to get F2, then F2 to get F1, then F1 to get F.
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Recursion
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Test Yourselves fib(0, 0). fib(1, 1). fib(X, Y) :- X > 1, X2 is X – 2, fib(X2, Y2), X1 is X – 1, fib(X1, Y1), Y is Y1 + Y2.
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 Fibonacci
  • 12. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 Example 1 Facts • mother_child(trude, sally). • father_child(tom, sally). • father_child(tom, erica). • father_child(mike, tom). Rules • sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). • parent_child(X, Y) :- father_child(X, Y). • parent_child(X, Y) :- mother_child(X, Y). Query • ?- sibling(sally, erica). Yes