# Tag Info

Accepted

### Iteration can replace Recursion?

It's possible to replace recursion by iteration plus unbounded memory. If you only have iteration (say, while loops) and a finite amount of memory, then all you have is a finite automaton. With a ...
Accepted

### Will this program terminate for every Integer?

The correct answer is that this function does not terminate for all integers (specifically, it does not terminate on -1). Your friend is correct in stating that this is pseudocode and pseudocode does ...
Accepted

### Why is tail recursion better than regular recursion?

if that is the only reason, why the compiler would not be able to optimize the regular recursive call? You are focusing on the wrong thing here: the reason the optimization works is because of the ...
• 5,151

### Iteration can replace Recursion?

Every recursion can be converted to iteration, as witnessed by your CPU, which executes arbitrary programs using a fetch-execute infinite iteration. This is a form of the Böhm-Jacopini theorem. ...
• 270k
Accepted

### Difference between Tail-Recursion and structural recursion

Structural recursion: recursive calls are made on structurally smaller arguments. Tail recursion: the recursive call is the last thing that happens. There is no requirement that the tail recursion ...
• 28.3k

### Iteration can replace Recursion?

As an example to the answer from Gilles, here is an "iterative" algorithm for the Ackermann function (using the common Ackermann-Péter version mentioned by Wikipedia $a(n,m)$). We need a stack $s$ of ...
Accepted

### Does the Y combinator contradict the Curry-Howard correspondence?

The original Curry-Howard correspondence is an isomorphism between intuitionistic propositional logic and the simply-typed lambda calculus. There are, of course, other Curry-Howard-like isomorphisms; ...
• 19.1k
Accepted

### Is the Berkeley tutorial on Fibonacci trees using wrong figures?

I would like to say both you and that Berkeley tutorial are correct. As commented by chepner, the trees in Berkeley tutorial and the trees you thought are the same semantically; only the labels of the ...
• 34.7k
Accepted

### Why are loops faster than recursion?

The reason that loops are faster than recursion is easy. A loop looks like this in assembly. ...
• 1,010

### Why are loops faster than recursion?

These other answers are somewhat misleading. I agree that they state implementation details that can explain this disparity, but they overstate the case. As correctly suggested by jmite, they are ...
• 11.8k

### Iteration can replace Recursion?

There are already some great answers (which I can't even hope to compete with), but I'd like to pitch this simple explanation. Recursion is just the manipulation of the runtime stack. Recursing adds ...
• 514
Accepted

### What property of cons allows elimination of tail recursion modulo cons?

While GCC likely uses ad-hoc rules, you can derive them in the following way. I'll use pow to illustrate since you're foo is so ...
• 11.8k
Accepted

### What's the Big O runtime of a DFS word search through a matrix?

The complexity will be $O(m*n*4^{s})$ where m is the no. of rows and n is the no. of columns in the 2D matrix and s is the length of the input string. When we start searching from a character we ...
• 1,145
Accepted

### If recursive Fibonacci is $O(2^N)$ then why do I get 15 calls for N=5?

You ask, I have an $O(2^n)$ runtime, why do I not observe $2^n$ recursive calls for $n=15$? There are many things wrong in the implied conclusion. $O(\_)$ only gives you an upper bound. The true ...
• 71k

### Why is tail recursion better than regular recursion?

The way a standard function/procedure (from now on I'll just say "function") call works is that the caller needs to store onto the stack whatever state it needs for computations that occur ...
• 19.1k
Accepted

### Is this a generic way to convert any recursive procedure to tail-recursion?

Your description of your algorithm is really too vague to evaluate it at this point. But, here are some things to consider. CPS In fact, there is a way to transform any code into a form that uses ...
• 632

### Correct name for a recursive descent parser that uses loops to handle left recursion?

It is just an LL(1) parser implemented with recursive descent. Starts with: ...
• 3,004
Accepted

### How can the class of tail recursive functions be compared to the classes of PR and R?

Every computable function can be expressed in continuation-passing-style, in which all calls are tail-calls. The trick is to add a "continuation" parameter to every function. Instead of making a non-...
• 29.2k
Accepted

### Count total number of k length paths in a tree

This can be solved in $\mathcal{O}(n \log n)$ by using the smaller-to-larger merging technique. Root the tree at an arbitrary vertex. We will calculate for every subtree an array where the $d$th ...
• 1,066

### Does the Y combinator contradict the Curry-Howard correspondence?

The Curry-Howard relates type systems to logical deduction systems. Among other things, it maps: programs to proofs program evaluation to transformations on proofs inhabited types to true ...

### What is most efficient for GCD?

For numbers that are small, the binary GCD algorithm is sufficient. GMP, a well maintained and real-world tested library, will switch to a special half GCD algorithm after passing a special threshold,...
• 553

### Can Breadth-First Search be Implemented Recursively without Data Structures?

You basically have two choices: "cheating" by embedding a queue in the nodes, and simulating BFS with higher complexity. Embedded-Queue Cheating If you look at virtually any description of BFS, e.g.,...
• 238

### What property of cons allows elimination of tail recursion modulo cons?

I’m going to beat around the bush for a while, but there is a point. Semigroups The answer is, the associative property of the binary reduction operation. That’s pretty abstract, but multiplication ...
• 1,121
Accepted

### How to derive dependently typed eliminators?

The canonical reference for this is Peter Dybjer, Inductive Families, which gives a pretty comprehensive treatment of inductive families based on eliminators.
• 7,744
Accepted

### Teaching Recursion

My favorite way to teach recursion is by reference to the Recursion Fairy. I'm sure we're all familiar with the idea that stories can be a very effective way to teach ideas; people seem built to hear ...
• 143k

### How is the complexity of recursive algorithms calculated and do they admit better complexity than non-recursive algorithms?

Typically, by writing a recurrence relation for the running time and then solving the recurrence relation. See How to come up with the runtime of algorithms? and Solving or approximating recurrence ...
• 143k
Accepted

### Algorithm to find maximum number of floors you can check with N eggs and D maximum drops

First we'll clarify the problem a bit. We have $n$ eggs, which we can drop from any floor we want. We also have the constraint that we are allowed a total of $d$ drops of these eggs (rather than $d$ ...
• 14.6k

### Is the Berkeley tutorial on Fibonacci trees using wrong figures?

Your trees are showing the same thing; you are just labeling each node by the call, and the Berkeley tutorial is labeling each node by the result of that call. Compare the two pictures of fibtree(3), ...
Accepted

### Space complexity analysis of binary recursive sum algorithm

In a given tree, all the vertices of this tree correspond to binarySum() calls. The value of parameter n to ...
• 2,977