Is recursion in code a meme ?

Is recursion in code a meme ?

Tell me of a problem that can not be done using for-loops but using a recursive function is the only solution

Attached: how-recursion-works-c%2B%2B.jpg (500x420, 37K)

Other urls found in this thread:

youtu.be/HXNhEYqFo0o
twitter.com/SFWRedditVideos

Is this a troll?

serious question, reading about it but I dont see why is it employed when for-loops are a thing. Is there something that I am missing here

I used recursion today on a client project.

Nested nav UI/routing structure that changeable via the CMS.

recursion is fine as long as you use tail recursion optimizations and/or don't go past the stack limit

Is for-loops in code a meme ?

Tell me of a problem that can not be done using if-statements but using for-loops is the only solution.


If you still don't understand, you're very silly.

nested objects like trees is an example where the recursive solution is the simplest. But yes, you can solve these problems without recursion using a current index array you push/pop as you traverse levels

Are for-loops a meme?

Tell me of a problem that cannot be done using the x86 instruction set but using for-loops is the only solution.

I dont see how the if-statement / for-loop relation links to the for-loop / recursion case

thanks for the clear answer, so searching a directory would be an ideal case for recursion

Attached: 1469408218792.jpg (957x621, 55K)

My point was that you can do almost anything with a giant pile of ugly if-statements. For-loops make many things a lot simpler and easier. In the same way, recursion makes a lot of things simpler and cleaner than using for-loops.

This is an answer from stack overflow;

>Yes and no. Ultimately, there's nothing recursion can compute that looping can't, but looping takes a lot more plumbing. Therefore, the one thing recursion can do that loops can't is make some tasks super easy.

>Take walking a tree. Walking a tree with recursion is stupid-easy. It's the most natural thing in the world. Walking a tree with loops is a lot less straightforward. You have to maintain a stack or some other data structure to keep track of what you've done.

>Often, the recursive solution to a problem is prettier. That's a technical term, and it matters.

thanks for the clarification

All recursive algorithms can be written iteratively, although they can lose a lot of intuition, e.g. postorder tree traversal or Towers of Hanoi. However, the recursive solution can be more efficient in some cases, such as using recursive matrix multiplication to calculate the nth Fibonacci number in O(log n) vs O(n) iteratively

I used to be uncomfortable with recursion, but when I got out of academia, it became a surprisingly handy tool for parsing janky tree structures. Recursion also mixes well with design patterns like composite.

Attached: 1523418141593.jpg (720x720, 140K)

do i need to learn tail recursion if im learning python? i read that python doesn't use it

For loops don't exist in assembly kiddo. Don't be ignorant, if you ever want to be good, you'll need to learn recursion.

youtu.be/HXNhEYqFo0o

Try writing binary search tree without recursion

>thanks for the clarification
Keep in mind that the ugly for loop is usually more performant

are for loops in code a meme?

Tell me of a problem that can not be done using recursion but using for loops is the only solution

Loops and recursion are equivalent anyway. But yes, there are thing that can only be computed using recursion. For example: the Ackerman function.

Print a binary tree for me, by height, N height.

find all the factors of number, find the factors of those factors, find the factors of those factors, etc. repeat this until you have only prime factors

From memory:
void
printbintree (bintree root)
{
stack s = newstack();
stackstash(s, root);
while (1) {
x = stackpop(s);
if (x != NULL) {
stackstash(s, x);
stackstash(s, x->left);
} else {
if (stackempty(s)) break;
x = stashpop(s);
printf ("%d\n", x->content);
stackstash(s, x->right);
}
}
freestack(s);
}

Wow, so hard.

It's more efficient in recursion.

Recursion is very code efficient in lots of situations, but is the absolute enemy of multithreading.
Therefore textbooks should barely even touch recursion. It's neat, but it has no place in a modern application.

You are now aware that a normal cpu doesn't have recursive opcodes, just calls to subroutines.

Yeah it's a meme. Tired of the whole we don't have to say how this algorithm works because recursion lol

Are there any compilers that don't use recursion?