0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Sec4

66

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

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

Views
Total Views
66
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
1
Embeds 0
No embeds

No notes for slide
• goalfact(4,X).
• 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
• Tracepow(2,4)i=4 Acc= 1 *2i=3 Acc = 2*2i=2 Acc= 2*2*2i=1 Acc= 2*2*2*2
• goalpower(2,4,P).
• Goal power(2,4,F).
• 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).
• Fib(4)i=4 first = 1 sec=1+1=2i=3 first=2sec=2+1=3i=2 first=3 sec=3+2=5
• goalfib(4,X).
• ### Transcript

• 1. Prolog Section 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.
• 3. 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
• 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
• 6. 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); }
• 7. 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. }
• 8. 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); }
• 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.
• 12. Factorial (tailer recursion) In C++ as if I’ll say: Acc=1; for(int i=X;i>0;i--) { Acc=Acc*i; }
• 13. 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).
• 14. 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).
• 15. Factorial (tailer recursion) * X F Acc 3 ? 1 2 1 ? ? 3 6 0 ? 6 = NAcc 3 6 6 NX 2 1 0
• 16. 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
• 17. 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).
• 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; }
• 21. 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).
• 22. 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
• 23. 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
• 24. 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.
• 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; }
• 26. 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).
• 27. Assignment Write a program to calculate the summation from X to Y of i