[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

What's the point of recursion when you can just use a while loop?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 21
Thread images: 3

File: 1337307425039.jpg (56KB, 468x317px) Image search: [Google]
1337307425039.jpg
56KB, 468x317px
What's the point of recursion when you can just use a while loop?
>>
While loops require state, which is inherently error-prone.
>>
>>60029775
>require state
What's state?

>error-prone
What's an example that will fail in a while loop but doesn't in a recursion
>>
>>60029709
What's the point of a loop, when you can just write a recursive function?
>>
>>60029775
Honestly, this is the most retarded sentence in the context of programming I have ever read in life.
>>
>>60029709
If you structure your recursive function properly, the compiler actually should translate it into a while loop (tail recursion).

Beyond that, it's a bit of an encapsulation thing, you know exactly what's going into and out of the function so it's easier to debug in a lot of situations.
>>
>>60029775
>>
>>60029837
But the poster was right, so if they were right and you disagree with them, you're an idiot who doesn't know what recursion or loops even really are.
>>
>>60029709

Use whichever one is most readable for the situation.

Often with a tree structure recursion comes naturally.
>>
>>60029709
It makes you feel smart
>>
>>60029866
>Often with a tree structure recursion comes naturally.

This.
>>
>>60029808
Whats the use of a recursive function if you can just copy and paste it
>>
>>60030256
>Whats the use of a recursive function if you can just copy and paste it
What? Are you actually mentally retarded, or just trying to be?
If you copy the function, you can't call into it a variable number of times.
>>
>>60030281
>not getting the joke
>calling others retarded
>>
>>60030482
> trying to make a joke
> failing hard
>>
>>60029709
>What's the point of recursion when you can just use a while loop?
Tree traversal made easier
>>
https://www.youtube.com/watch?v=Qk0zUZW-U_M

the pajeets has evolved
>>
With TCO those two codes are equivalent.
static int rec_fact_loop (int accu, int n) {
if (n <= 1) {
return accu;
} else {
return rec_fact_loop (accu * n, n - 1);
}
}

int rec_fact (int n) {
return rec_fact_loop (1, n);
}

int while_fact (int n) {
int res;
res = 1;
while (n > 1) {
res *= n;
n--;
}
return res;
}
>>
File: 1473617178949.png (251KB, 1190x906px) Image search: [Google]
1473617178949.png
251KB, 1190x906px
>>60029775
>>
>>60029709
Because loops are inelegant as sh*t!
>>
>>60029775
congratulations you dumb fuck
Thread posts: 21
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.