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
Here is a program to calculate the summation from X to Y of i using tail recursive logic in Prolog:
predicates
sum(integer, integer, integer)
sum_aux(integer, integer, integer, integer)
clauses
sum(X, Y, Sum):- sum_aux(X, Y, Sum, 0).
sum_aux(X, X, Sum, Sum):- !.
sum_aux(X, Y, Sum, Acc):-
NewX is X + 1,
NewAcc is Acc + NewX,
NewY is Y,
sum_aux(NewX, NewY, Sum, NewAcc).
This defines sum/3
Ä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
Ä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)
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
2. 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.
4. 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
5. 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
9. 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
10. 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
11. 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.
18. 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
19. 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*???;
}
20. 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;
}
25. 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;
}
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!
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
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
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.
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).
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).
Goal factorial(3,F).
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