Questions tagged [tail-recursion]
12 questions
16
votes
2 answers
What property of cons allows elimination of tail recursion modulo cons?
I'm familiar with the idea of basic tail recursion elimination, where functions that return the direct result of a call to themselves can be rewritten as iterative loops.
foo(...):
# ...
return foo(...)
I also understand that, as a special…
Maxpm
- 263
- 1
- 7
13
votes
4 answers
Why is tail recursion better than regular recursion?
There is the axiom you should always prefer tail-recursion over regular recursion whenever possible. (I'm not considering tabulation as an alternative in this question).
I understand the idea by why is that the case? Is it only because of the…
AmandaSai98b
- 251
- 2
- 5
6
votes
1 answer
How can the class of tail recursive functions be compared to the classes of PR and R?
How can the class of tail recursive functions (TR) be compared to the classes of primitive recursive functions (PR) and recursive functions (R)?
The computation of a PR function always halts. This does not apply to TR functions.
Given a tail…
Peter
- 361
- 1
- 5
4
votes
1 answer
Tail call optimization via translating to CPS
I am struggling to wrap my head around this compiler technique, so let's say here's my factorial function
def factorial(value: int) -> int:
if value == 0:
return 1
else:
return factorial(value-1) * value
It is recursive, but…
hello world
- 235
- 1
- 4
3
votes
1 answer
Loop optimization of non-tail recursion
When researching how to optimize recursion into loops, I came upon (on Wikipedia) a general rule about this: Whenever a function is in form:
fn F(x):
if p(x):
return F(a(x))
else:
return b(x)
Then it may be written as:
fn F(x):
while…
lav_shaun
- 53
- 3
3
votes
1 answer
Tail recursion can't work with dynamic programming programs
I am doing some exercises on dynamic programming in order to get familar with this concept.
I've noticed that most of the time it's not difficult to calculate the complexity of a program using dynamic programming since most of the time we are…
DP_q
- 31
- 2
2
votes
1 answer
Iteration over long linked lists and trees instead recursion
For example exist linked list with thousands (maybe million) elements or very deep tree which some branches are very long and have only one child, but exists other short branches with many children. How process this lists and trees changing very…
Saku
- 141
- 4
1
vote
2 answers
What is the relationship between tail recursion with other recursions?
I'm rather confused by the recursion theory.
From the link, the recursion theory was formed by Dedekind, Gödel and some other famous mathematicians. There are the following types of recursion. But where is tail recursion? can this concept parallel…
user3329081
- 111
- 1
1
vote
0 answers
Can call stack always be eliminated for singly recursive functions?
Consider a singly recursive function $f$, which, say, has the following form:
f(a):
if a is some base case: return something
b = pre-processing(a)
c = f(b)
d = post-processing(a,b,c)
return d
I assume that pre-processing and…
Bruno
- 346
- 1
- 8
0
votes
1 answer
Structural induction proof for reverse(push(as, bs)) = push(reverse(bs), reverse(as))
I need to prove:
reverse(push(as,bs)) = push(reverse(bs), reverse(as))
where:
def push[T](as: List[T], bs: List[T]):
List [T] = as match {
case Nil => bs
case x::xs => x::push(xs, bs)
}
def reverse[T](ls: List[T]):
List[T] = ls…
0
votes
0 answers
Are some algorithms inherently recursive?
Are some algorithms inherently recursive?
As in, rewriting it in tail-recursive/iterative form with a stack is still needed, and there is no way to do it otherwise.
I am asking because I struggled to implement the function stringsMatchingPrefix on a…
blitzqwe
- 125
- 2
-1
votes
1 answer
Stack depth for QuickSort
CLRS Problem : 7.4
How does Tail-Recursive-QuickSort improve the efficiency of quick sort any better ?
Original quicksort
Tail recursive quicksort
Question
Modify the code for TAIL-RECURSIVE-QUICKSORT so that the worst-case stack depth is O(n…
sagar
- 101
- 1
- 2