SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Prolog
Section 4
Recursion
• A common method of simplification is to
divide a problem into subproblems of the
same type.
• Recursion is when a function calls itself.
• It is when a function (operation) exists on the
left and the right hand side of an equation.
• It should have a stop condition.
Factorial
Iterative

Recursive

X! = 1*2*3*…*X
= X*(X-1)*(X-2)*…*2*1

X!=

Factorial (X-1)

1
if X=0
X*(X-1)! otherwise
Factorial
Iterative prolog, there are no return values -> new
Recursive
•In
the result
int factorial(int X) variable forint factorial(int X)
{

}

factorial(X,Y) -> we’ll put the result in Y Basic step
{
int Y=1;
if(X==0)
•The if statements in C++ are converted into
for(int i=0;i<X;i++)
return 1;
rules in prolog
{
else
Y*=X-i;
return X*factorial(X-1);
}
}
General
return Y;
rule
Factorial
•We
Iterative can’t put an operation as a parameter to a
Recursive
rule. factorial(int X)
int factorial(int X)
int
factorial(X-1)  Z=X-1, factorial(Z)
{
{
int Y=1;
if(X==0)
•Bounded variables can’t be assigned a value
for(int i=0;i<X;i++)
return 1;
X=5
{
else
Y*=X-i;
return X*factorial(X-1);
If X is free
If X is bounded
}
} then this statement is
then this statement is
return Y;
assign statement
comparison statement,
}
and returns true or false
Factorial
(non-tailer recursion)
predicates
fact(integer,integer)
clauses
fact(X,Y):-X=0,Y=1.
fact(X,Y):- Z=X-1,
fact(Z,NZ),Y=NZ*X.

Recursive

int factorial(int X)
{
if(X==0)
return 1;
else
return
X*factorial(X-1);
}
Factorial
(non-tailer recursion)
Recursive
predicates
int factorial(int X)
fact(integer,integer)
Stack overflow {
clauses
(infinite loop)
if(X==0)
fact(0,1).
return 1;

else
return
X*factorial(X-1);

fact(X,Y):- Z=X-1,
fact(Z,NZ),Y=NZ*X.
}
Factorial
(non-tailer recursion)
predicates
fact(integer,integer)
clauses
fact(0,1):-!.
fact(X,Y):- Z=X-1,
fact(Z,NZ),Y=NZ*X.

Recursive

int factorial(int X)
{
if(X==0)
return 1;
else
return
X*factorial(X-1);
}
Tracing
fact(4,Y).

0

Y=24

Match with the second rule:

6
6
fact (4, 24 ):- Z=4-1=3, fact (3,NZ),Y=NZ*4.
Y
Match with the second rule:

fact (3,6):- Z=3-1=2, fact (2,NZ),Y=NZ*3.
Y
2
2
Match with the second rule:

fact (2,2):- Z=2-1=1, fact (1,NZ),Y=NZ*2.
Y
1
1
Match with the second rule:

fact (1,1):- Z=1-1=0, fact (0,NZ),Y=NZ*1.
Y
1
1
Match with the first rule:

fact (0,1).

Then Y=1
Tracing
fact(4,Y).

Y=24

Match with the second rule:

6
6
fact TheYproblem with non-tailer recursion is
(4, 24 ):- Z=4-1=3, fact (3,NZ),Y=NZ*4.

Match with the second rule: variable waiting for
that there is always a

fact (3,6):- Z=3-1=2, fact (2,NZ),Y=NZ*3. call
Y
2
2
the return value from the recursive
Match withto complete the computations
the second rule:
fact (2,2):- Z=2-1=1, fact (1,NZ),Y=NZ*2.
Y
1
1
Match with the second rule:
(lots of memory taken… not good!)
fact (1,1):- Z=1-1=0, fact (0,NZ),Y=NZ*1.
Y
1
1
Match with the first rule:

fact (0,1).

Then Y=1
Tailer recursion
• Put the recursive call at the tail.
• To convert from non-tailer to tailer recursion
we need:
– Auxiliary (helper) function
– Accumulator

• Instead of waiting for the returned value I’ll
accumulate the result, so each time I have a
recursive call I’ll be sending part of the result
until the computations are completed.
Factorial
(tailer recursion)
In C++ as if I’ll say:
Acc=1;
for(int i=X;i>0;i--)
{
Acc=Acc*i;
}
Factorial
(tailer recursion)
predicates
fact(integer,integer)
fact_aux(integer,integer,integer)
clauses
fact(X,F):-fact_aux(X,F,1).
fact_aux(0,F,Acc):- F=Acc,!.
fact_aux(X,F,Acc):NAcc=Acc*X, NX=X-1,
fact_aux(NX,F,NAcc).
Factorial
(tailer recursion)
predicates
fact(integer,integer)
fact_aux(integer,integer,integer)
clauses
fact(X,F):-fact_aux(X,F,1).
fact_aux(0,F,F):-!.
fact_aux(X,F,Acc):NAcc=Acc*X, NX=X-1,
fact_aux(NX,F,NAcc).
Factorial
(tailer recursion)

*
X

F

Acc

3

?

1

2
1

?
?

3
6

0

?

6

= NAcc
3
6
6

NX
2
1
0
Power
Iterative

Recursive

23 = 2*2*2
XN=X*X*…*X (N times)

XN =

X (N-1)

1
if N=0
X*XN-1 otherwise
Power
(non-tailer recursion)
predicates
power(integer,integer,integer)
clauses
power(X,0,1):-!.
power(X,N,P):-Z=N-1,
power(X,Z,NP),P=NP*X.
goal
power(2,4,X).
Tracing
power(2,4,P). P=16
Match with the second rule:
16
8
8
power(2,4, P ):-Z=4-1=3, power(2,3,NP),P=NP*2.

Match with the second rule:

4
4
power(2,3,P):-Z=3-1=2, power(2,2,NP),P=NP*2.
8
Match with the second rule:

power(2,2,P):- Z=2-1=1, power(2,1,NP),P=NP*2.
4
2
2
Match with the second rule:

power(2,1,P):- Z=1-1=0, power(2,0,NP),P=NP*2.
2
1
1
Match with the first rule:

power(2,0,1).

Then P=1
Power
(tailer recursion)
In C++ as if I’ll say:
Acc=1;//in case of multiplication initialize the accumulator
with 1 but in case of addition initialize it with zero

for(int i=N;i>0;i--)
{
Acc=Acc*???;
}
Power
(tailer recursion)
In C++ as if I’ll say:
Acc=1;//in case of multiplication initialize the accumulator
with 1 but in case of addition initialize it with zero

for(int i=N;i>0;i--)
{
Acc=Acc*X;
}
Power
(tailer recursion)
predicates
power(integer,integer,integer)
power_aux(integer,integer,integer,integer)
clauses
power(X,N,P):- power_aux(X,N,P,1).
power_aux(_,0,P,P):-!.
power_aux(X,N,P,Acc):Nacc=Acc*X, Z=N-1,
power_aux(X,Z,P,Nacc).
Power
(tailer recursion)

*
X

N

P

Acc

2

4

?

1

2
2

3
2

?
?

2
4

2
2

1
0

?
?

8
16

= NAcc
2
4
8
16

Z (new N)
3
2
1
0
Fibonacci
fib(0)=1
fib(1)=1
fib(X)=fib(X-1)+fib(X-2)
Ex:
X
F(X)

0
1

1
1

2
2

3
3

F(X)=F(X1)+F(X2)

4
5

5
8
Fibonacci
(non-tailer recursion)
predicates
fib(integer,integer)
clauses
fib(1,1):-!.
fib(0,1):-!.
fib(X,Y):M=X-1,N=X-2,
fib(M,B),fib(N,A),Y=A+B.
Fibonacci
(tailer recursion)
int fib (int X)
{
int first = 1;
int second = 1;
for(int i = X; i>1; i--)
{
int temp=first;
first = second;
second = temp + second;
}
return second;
}
Fibonacci
(tailer recursion)
predicates
fib(integer, integer)
fib_aux(integer, integer, integer, integer)
clauses
fib(X, Fib):fib_aux(X, Fib,1, 1).
fib_aux(1, Second,_, Second):-!.
fib_aux(X, Fib,First, Second):NewX = X - 1,
NewFirst = Second,
NewSecond = First + Second,
fib_aux(NewX, Fib, NewFirst, NewSecond).
Assignment
Write a program to calculate the summation
from X to Y of i

Weitere ähnliche Inhalte

Was ist angesagt?

Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Chyi-Tsong Chen
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
MATLAB ODE
MATLAB ODEMATLAB ODE
MATLAB ODEKris014
 
Boolean Matching in Logic Synthesis
Boolean Matching in Logic SynthesisBoolean Matching in Logic Synthesis
Boolean Matching in Logic SynthesisIffat Anjum
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...The1 Uploader
 
Regression_1.pdf
Regression_1.pdfRegression_1.pdf
Regression_1.pdfAmir Saleh
 
Principle of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun UmraoPrinciple of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun Umraossuserd6b1fd
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Introduction to Decision Making Theory
Introduction to Decision Making TheoryIntroduction to Decision Making Theory
Introduction to Decision Making TheoryYosuke YASUDA
 
Eigenvalue eigenvector slides
Eigenvalue eigenvector slidesEigenvalue eigenvector slides
Eigenvalue eigenvector slidesAmanSaeed11
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Modern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsModern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsAmr E. Mohamed
 
Numarical values highlighted
Numarical values highlightedNumarical values highlighted
Numarical values highlightedAmanSaeed11
 
Rules of derivatives 2.2
Rules of derivatives 2.2Rules of derivatives 2.2
Rules of derivatives 2.2Lorie Blickhan
 
Optimization Approach to Nash Euilibria with Applications to Interchangeability
Optimization Approach to Nash Euilibria with Applications to InterchangeabilityOptimization Approach to Nash Euilibria with Applications to Interchangeability
Optimization Approach to Nash Euilibria with Applications to InterchangeabilityYosuke YASUDA
 

Was ist angesagt? (20)

Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
Intelligent Process Control Using Neural Fuzzy Techniques ~陳奇中教授演講投影片
 
21 3 ztransform
21 3 ztransform21 3 ztransform
21 3 ztransform
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
MATLAB ODE
MATLAB ODEMATLAB ODE
MATLAB ODE
 
Chap08alg
Chap08algChap08alg
Chap08alg
 
Lyapunov stability
Lyapunov stability Lyapunov stability
Lyapunov stability
 
Boolean Matching in Logic Synthesis
Boolean Matching in Logic SynthesisBoolean Matching in Logic Synthesis
Boolean Matching in Logic Synthesis
 
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
Pengolahan Data Panel Logit di Stata: Penilaian Goodness of Fit, Uji Model, d...
 
Finite difference &amp; interpolation
Finite difference &amp; interpolationFinite difference &amp; interpolation
Finite difference &amp; interpolation
 
Regression_1.pdf
Regression_1.pdfRegression_1.pdf
Regression_1.pdf
 
Principle of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun UmraoPrinciple of Function Analysis - by Arun Umrao
Principle of Function Analysis - by Arun Umrao
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Introduction to Decision Making Theory
Introduction to Decision Making TheoryIntroduction to Decision Making Theory
Introduction to Decision Making Theory
 
Eigenvalue eigenvector slides
Eigenvalue eigenvector slidesEigenvalue eigenvector slides
Eigenvalue eigenvector slides
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Modern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsModern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI Systems
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Numarical values highlighted
Numarical values highlightedNumarical values highlighted
Numarical values highlighted
 
Rules of derivatives 2.2
Rules of derivatives 2.2Rules of derivatives 2.2
Rules of derivatives 2.2
 
Optimization Approach to Nash Euilibria with Applications to Interchangeability
Optimization Approach to Nash Euilibria with Applications to InterchangeabilityOptimization Approach to Nash Euilibria with Applications to Interchangeability
Optimization Approach to Nash Euilibria with Applications to Interchangeability
 

Andere mochten auch

Gianotti María de los Ángeles Unidad 1
Gianotti María de los Ángeles Unidad 1Gianotti María de los Ángeles Unidad 1
Gianotti María de los Ángeles Unidad 1mariangianotti9
 
West Suburban Teachers Union 75th anniversary presentation
West Suburban Teachers Union 75th anniversary presentationWest Suburban Teachers Union 75th anniversary presentation
West Suburban Teachers Union 75th anniversary presentationa bendelow
 
Què és la infor.
Què és la infor.Què és la infor.
Què és la infor.evamm24
 
The stages of film making
The stages of film makingThe stages of film making
The stages of film makingbenzibob1234
 
Tp de ingles
Tp de inglesTp de ingles
Tp de inglesSofia_T
 
Bintang menurut perspektif al quran
Bintang menurut perspektif al   quranBintang menurut perspektif al   quran
Bintang menurut perspektif al quranfarisiman0821
 
VisióN Comparativa De Una Unidad De InformacióN En
VisióN Comparativa De Una Unidad De InformacióN EnVisióN Comparativa De Una Unidad De InformacióN En
VisióN Comparativa De Una Unidad De InformacióN Enjuliemarie_7
 
practicas pedagogicas
practicas pedagogicaspracticas pedagogicas
practicas pedagogicasjeff120
 

Andere mochten auch (9)

Gianotti María de los Ángeles Unidad 1
Gianotti María de los Ángeles Unidad 1Gianotti María de los Ángeles Unidad 1
Gianotti María de los Ángeles Unidad 1
 
West Suburban Teachers Union 75th anniversary presentation
West Suburban Teachers Union 75th anniversary presentationWest Suburban Teachers Union 75th anniversary presentation
West Suburban Teachers Union 75th anniversary presentation
 
Què és la infor.
Què és la infor.Què és la infor.
Què és la infor.
 
The stages of film making
The stages of film makingThe stages of film making
The stages of film making
 
Tp de ingles
Tp de inglesTp de ingles
Tp de ingles
 
Bintang menurut perspektif al quran
Bintang menurut perspektif al   quranBintang menurut perspektif al   quran
Bintang menurut perspektif al quran
 
VisióN Comparativa De Una Unidad De InformacióN En
VisióN Comparativa De Una Unidad De InformacióN EnVisióN Comparativa De Una Unidad De InformacióN En
VisióN Comparativa De Una Unidad De InformacióN En
 
104 O Haver Handout
104 O Haver Handout104 O Haver Handout
104 O Haver Handout
 
practicas pedagogicas
practicas pedagogicaspracticas pedagogicas
practicas pedagogicas
 

Ähnlich wie Here is a Prolog program to calculate the summation from X to Y of i:sum(X, Y, Sum) :- summation(X, Y, 0, Sum).summation(X, X, RunningSum, RunningSum).summation(I, Y, RunningSum, TotalSum) :- NewRunningSum is RunningSum + I, NewI is I + 1, summation(NewI, Y, NewRunningSum, TotalSum).To use it:?- sum(1, 5, Sum).Sum = 15This defines two predicates - sum/3 to calculate the total sum given the lower and upper bounds, and summation/4

Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functionsswartzje
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introductionSSE_AndyLi
 
Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Ilir Destani
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdfssuser3a8f33
 
Recursion examples
Recursion examplesRecursion examples
Recursion examplesHafsaZahran
 
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksStratio
 
Answers To Exercises Microeconomic Analysis Third Edition
Answers To Exercises Microeconomic Analysis Third EditionAnswers To Exercises Microeconomic Analysis Third Edition
Answers To Exercises Microeconomic Analysis Third EditionStephen Faucher
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookJosé Antonio PAYANO YALE
 
Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Yuan Jing
 

Ähnlich wie Here is a Prolog program to calculate the summation from X to Y of i:sum(X, Y, Sum) :- summation(X, Y, 0, Sum).summation(X, X, RunningSum, RunningSum).summation(I, Y, RunningSum, TotalSum) :- NewRunningSum is RunningSum + I, NewI is I + 1, summation(NewI, Y, NewRunningSum, TotalSum).To use it:?- sum(1, 5, Sum).Sum = 15This defines two predicates - sum/3 to calculate the total sum given the lower and upper bounds, and summation/4 (20)

Recursion
RecursionRecursion
Recursion
 
Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Recursion
RecursionRecursion
Recursion
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functions
 
Recursion
RecursionRecursion
Recursion
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Generating functions (albert r. meyer)
Generating functions (albert r. meyer)Generating functions (albert r. meyer)
Generating functions (albert r. meyer)
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
 
Recursion examples
Recursion examplesRecursion examples
Recursion examples
 
6-Python-Recursion.pdf
6-Python-Recursion.pdf6-Python-Recursion.pdf
6-Python-Recursion.pdf
 
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural Networks
 
Answers To Exercises Microeconomic Analysis Third Edition
Answers To Exercises Microeconomic Analysis Third EditionAnswers To Exercises Microeconomic Analysis Third Edition
Answers To Exercises Microeconomic Analysis Third Edition
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Neural Networks.pptx
Neural Networks.pptxNeural Networks.pptx
Neural Networks.pptx
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution book
 
14 recursion
14 recursion14 recursion
14 recursion
 
Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...Comparison market implied volatilities with implied volatilities computed by ...
Comparison market implied volatilities with implied volatilities computed by ...
 
opt_slides_ump.pdf
opt_slides_ump.pdfopt_slides_ump.pdf
opt_slides_ump.pdf
 

Kürzlich hochgeladen

Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Kürzlich hochgeladen (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

Here is a Prolog program to calculate the summation from X to Y of i:sum(X, Y, Sum) :- summation(X, Y, 0, Sum).summation(X, X, RunningSum, RunningSum).summation(I, Y, RunningSum, TotalSum) :- NewRunningSum is RunningSum + I, NewI is I + 1, summation(NewI, Y, NewRunningSum, TotalSum).To use it:?- sum(1, 5, Sum).Sum = 15This defines two predicates - sum/3 to calculate the total sum given the lower and upper bounds, and summation/4

Hinweis der Redaktion

  1. goalfact(4,X).
  2. If the goal is what question, ex factorial(4,Y), when it reaches X=1 it will match with the first rule AND also the second trying to subtract 1 from X so it will be zero then -1 then -2… so on!
  3. Tracing the goal factorial(4,Y)The problem with non-tailer recursion is that it is always waiting for the return value to complete the computations
  4. Tracing the goal factorial(4,Y)The problem with non-tailer recursion is that it is always waiting for the return value to complete the computations
  5. To convert non-tailer to tailer, I’ll need a helper function in which I’ll define a new variable to accumulate the results in.
  6. Why do I need the auxiliary function? because the user should not know anything about the accumulator, he ONLY knows the number and want its factorial.goalfact(4,X).
  7. Why do I need the auxiliary function? because the user should not know anything about the accumulator, he ONLY knows the number and want its factorial.goalfact(4,X).
  8. Goal factorial(3,F).
  9. Tracing the goal Power(2,4,P)The problem with non-tailer recursion is that it is always waiting for the return value to complete the computations
  10. Tracepow(2,4)i=4 Acc= 1 *2i=3 Acc = 2*2i=2 Acc= 2*2*2i=1 Acc= 2*2*2*2
  11. goalpower(2,4,P).
  12. Goal power(2,4,F).
  13. Why put rule for both fib(0) and fib(1)? Because if the user asks for fib(0) it must have a rule to match with.goalfib(6,X).
  14. Fib(4)i=4 first = 1 sec=1+1=2i=3 first=2sec=2+1=3i=2 first=3 sec=3+2=5
  15. goalfib(4,X).