question about last call optimization
- From: "blindsearch" <dpatte3@xxxxxxxxxxx>
- Date: 26 Jul 2005 15:47:16 -0700
I have a question about last call optimization using euclids algorithm
as an example. Okay the prolog literature gives the procedure as:
gcd(A,0,A).
gcd(A,B,GCD) :- B > 0, C is A mod B, gcd(B,C,GCD).
Now will prolog "last call optimize" this. It is easy to see that when
prolog gets to the goal gcd(B,C,GCD) in the last clause that there are
no alternative clauses. I am assuming then that this procedure is "last
call optimized". Is this true?
What about if we switch the order of the clauses as such:
gcd(A,B,GCD) :- B > 0, C is A mod B, gcd(B,C,GCD).
gcd(A,0,A).
Now upon reaching the last goal in the first clause, prolog can not
determine that there are no alternative clauses. Right?
Okay finally, say we write:
gcd(A,0,GCD) :- !, GCD = A.
gcd(A,B,GCD) :- T is A mod B, gcd(B,T,GCD).
Now, this should be "last call optimized" right? So in terms of speed
of execution would this last example execute equivalently to the first?
What about to the second?
One more question. There is no big difference between the three
versions of gcd. The first two do leave behind some choicepoints
whereas the last does not leave any behind. So is there any point in
writing it the last way? We're not supposed to use cuts all the time
right? or is it a good habit to get into in the event that we run into
procedures that have the potential of leaving behind a lot of choice
points?
Okay wait, one more question, Am I analyzing this too much?
.
- Follow-Ups:
- Re: question about last call optimization
- From: Neng-Fa Zhou
- Re: question about last call optimization
- Prev by Date: Re: 用TP20, VP51生花, 一切出於六合彩開始
- Next by Date: language of postings (was Re: turbo prolog or visual prolog)
- Previous by thread: Prolog Programmers Needed
- Next by thread: Re: question about last call optimization
- Index(es):
Relevant Pages
|