54 votes
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 ...
user avatar
49 votes
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 ...
user avatar
48 votes
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 ...
user avatar
34 votes

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. ...
user avatar
28 votes
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 ...
user avatar
  • 28.3k
25 votes

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 ...
user avatar
21 votes
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; ...
user avatar
  • 19.1k
21 votes
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 ...
user avatar
  • 34.7k
19 votes
Accepted

Why are loops faster than recursion?

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

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 ...
user avatar
16 votes

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 ...
user avatar
  • 514
15 votes
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 ...
user avatar
15 votes
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 ...
user avatar
  • 1,145
13 votes
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 ...
user avatar
  • 71k
13 votes

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 ...
user avatar
  • 19.1k
12 votes
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 ...
user avatar
11 votes

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: ...
user avatar
  • 3,004
10 votes
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-...
user avatar
  • 29.2k
10 votes
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 ...
user avatar
9 votes

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 ...
user avatar
9 votes

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,...
user avatar
  • 553
8 votes

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.,...
user avatar
8 votes

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 ...
user avatar
  • 1,121
8 votes
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.
user avatar
  • 7,744
7 votes
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 ...
user avatar
  • 143k
7 votes

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 ...
user avatar
  • 143k
7 votes
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$ ...
user avatar
  • 14.6k
7 votes

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), ...
user avatar
6 votes
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 ...
user avatar
  • 2,977
6 votes
Accepted

Undecidable definition of pure function?

On undecidability You attempt to prove that fibonacci is pure in a particular way, and you fail to do so. This does not prove that purity is an undecidable ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible