![]() |
Anyone a genius?
linearly recursive functions can always be transformed into tail recursive functions.
Then disprove ^. After hours of scouring the intrawebs, I am no closer to any hope of answering that question. In fact, most of the websites I encountered contradict each other. But I can rattle off the advantages and disadvantages of each, and how modern compilers should implement ways to get rid of tail recursion. And yes, this is homework. |
Re: Anyone a genius?
You are. I see you viewing this.
|
Re: Anyone a genius?
"linearly recursive functions" is a term I'm not familiar with. Is this a special form of recursive functions, similar to how tail recursive functions are?
|
Re: Anyone a genius?
A linearly recursive function is one in which one recursive call is made, but the function that makes the recursive call has to wait to return its value. One example would be if the return looked like
n * ICalledMyselfAgain(); In the above example, the function cannot immediately return because it has to wait for its recursive call to return so that it can multiply by n. This takes up more stack space the more recursive calls are made. Tail recursion only makes one recursive call, like linear recursion. But the return value in tail recursion is the result... there is no other operation on it. So tail recursion would look like ICalledMyselfAgain(); |
Re: Anyone a genius?
Not quite so, Fallback Son. Tail recursion simply means that the last operation in a function is a call to itself. So;
:
int CallMySelfAgain(int i) |
Re: Anyone a genius?
After having just completed two semesters of Scheme programming, I can tell you that it is indeed possible to transform any algorithm into Tail-Recursion if you use the Continuation Passing Style algorithm.
http://lambda-the-ultimate.org/node/2673#comment-40136 Notes from one of my class's lectures: :
; Basics: |
Re: Anyone a genius?
Quote:
Also, I'm glad you gave that example in particular, I never considered whether a linearly recursive function which calls another function can always be written tail recursively. But I imagine those can also. |
Re: Anyone a genius?
Quote:
|
Re: Anyone a genius?
Like I said, all recursive algorithms can be written in continuation passing style - which is similar to tail recursion (and often achieves the same effects in terms of efficiency). Not everything can be written in straight tail recursion (that I know of). There's a whole field of study called Lambda Calculus - it's used to prove attributes of algorithms like this.
http://en.wikipedia.org/wiki/Lambda_calculus |
Re: Anyone a genius?
No... your first post said that it is possible to transform any algorithm into tail recursion using CPS. Your second post said its possible to transform any algorithm into something "similar" to tail recursion using CPS. Those are different. Also, from my study of CPS, I vaguely recall it saying that CPS transforms linear recursion into tail recursion THEN into iteration. So this would indicate that your first post was, in fact, correct.
|
| All times are GMT -5. The time now is 12:51 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC