[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: recur.jpg (6 KB, 332x83)
6 KB
6 KB JPG
If recursion is so good why are computers so bad at it?
>>
>>108436365
>recursion is so good
says who?
>>
>>108436365
>computers are so bad at it
says who?
>>
>>108436365
What even is tco.
get out of /g/ you fucking tourist.
>>
>>108436410
>it's good because it needs optimization to be good
>>
It isn't really "so good" or even used often in an enterprise setting, it's a party trick for CS teachers to scare the newbies. There tend to be far more efficient ways to do it iteratively or in parallel. The exception is navigating tree/fractal structures.
>>
>>108436418
it has no cost if your language is tco and maps perfectly to some class of problems.
using recursion and functional programming for everything is retarded, but so is not using it for anything.
use the thing that maps the best to the problem you are trying to solve.
>>
>>108436365
It was 'good' on a handful of largely unsuccessful stack machine architectures that were used at universities in the '60s, and that brain damaged the students who learned on them then later went on to be textbook writers.
>>
>>108436365
>thing must be good or bad
Black and White thinking is for retards.
As any CS undergrad should be able to tell you, recursion is expressive. Often, you can use a recursive algorithm to solve a problem elegantly in a few lines of code. The equivalent iteration would be longer and more complex.
But that same CS undergrad should also be able to tell you about function call overhead and blowing the stack.

Tbqf there are two main reasons recursion gets overhyped.

1. It's one of the simplest tard filters.
2. Functional languages.
>>
>>108436426
this i dont remember last time i used recursion in production code. besides tail-recursion which is just fancy loop anyway
>>
I've programming for 2 years, and I don't get why recursion is useful. Is it? When should I use it?
>>
>>108436418
>tech illiterate retard sees "optimization" in an acronym and its brain shuts off
>>
>eight posts in
>only one response even gets close
nu/g/, I swear to god.

It's actually very simple if you understand how recursion is implemented on an instruction level. In loops you have your loop body, and that body is optimized to avoid as many memory accesses (call and ret, push and pop) as possible. Memory accesses are slow, and recursion has to keep the state of previous iterations in memory, which not only means a lot of call and ret and push and pop, but also has a good chance to grow beyond the CPU caches. And while prefetching might be good at predicting the linear access patterns of your recursion it cannot get rid of the memory bandwidth bottleneck, only some of its latency.
>>
>>108436588

And this is very bad when it's a service handling thousands and millions of requests a day, max memory can be reached quite easily if this is your go to pattern
>>
>>108436576
the one time i finally found a use case for recursion was ruined because it took too much memory
>>
>>108436588
you are thinking of the most basic recursion possible where x_n = x_(n-1)
what's the use case for that? just use for loops
>>
>>108436377
functional shills.
>>
>>108436410
Not everything can be tail-recursive.
>>
File: je3tkll.png (245 KB, 422x592)
245 KB
245 KB PNG
kek OP wtf
>>
File: fn1.jpg (19 KB, 405x207)
19 KB
19 KB JPG
OP did you make a mistake? Was it supposed to be f(n-1)
>>
>>108436866
>>108436845
One of the strengths of recursion is that it sometimes the entire thing blows up for certain inputs :^)
>>
>>108436830
How is it any different for more complicated recursion?
>>
>>108437157
why don't you try drawing a recursion tree on the blackboard and thinking about it yourself?
>>
>>108437593
>no argument
I accept your concession.
>>
Back in the day understanding recursion was enough to guarantee you a 300k/year tech job. Gen X had it so easy.
>>
If you think functional programming languages underspecify if they optimize tail-call recurison, just wait til you try to look up how they handle tail modulo cons.
Recursion on user input should just be considered a security vulnerability.
>>
>>108436581
Ask an LLM bro, its statistical approximation will get it dead right. This been talked to death.
>>
>>108437607
>Back in the day understanding recursion
was actually useful and the IQ hadn't dropped so far the students needed AI to learn it
>>
>>108436581
recursion just means that a function can call itself. its useful when working with data structures that contain nodes, like graphs and lists. dfs algorithms are also naturally written with recursion vs bfs which do not. you can also build a return value using stack frames. here is a stupid simple example in c.
#include <stdio.h>
#include <stdlib.h>

struct node {
int val;
struct node* next;
};

void free_list(struct node* head)
{
if (!head)
return;
struct node* next = head->next;
free(head);
free_list(next);
}

struct node* cons(int val, struct node* head)
{
struct node* n = malloc(sizeof(struct node));
*n = (struct node){ .val = val, .next = head };
return n;
}

int add_one(int val)
{
return val + 1;
}

struct node* map(int (*f)(int), struct node* head)
{
if (!head)
return NULL;
return cons(f(head->val), map(f, head->next));
}

void print_list(struct node* head)
{
if (!head) {
printf("\n");
return;
}
printf("%d ", head->val);
print_list(head->next);
}

int main()
{
struct node* list = cons(1, cons(2, cons(3, cons(4, cons(5, NULL)))));
struct node* inc_list = map(add_one, list);
print_list(list);
print_list(inc_list);
free_list(list);
free_list(inc_list);
return 0;
}
>>
>>108436365
Recursion is bad. It's confusing.
>noooo you're just a brainlet!
Yes yes very nice. But I am not a brainlet. I have written many recursive functions. It's not worth it except for aesthetics or code golf. Those things are fine, but if I am trying to write good and maintainable software I will pretty much never use recursion.
>>
>>108436377
computers
>>
>>108440405
>But I am not a brainlet
Yes you are, and no amount of word salad will never change that.
>>
>>108440405
When I was learning programming many of the most common examples used to demonstrate recursion (factorial, fibonacci) were far less obvious than the iterative solutions and that really turned me off the technique until I took a data structures course and had to deal with trees. Recursion really is a natural fit for some problems.
>>
Should I do it bro and take the CS plunge
>>
Recursion is literally the same as an imperative solution. If you disagree, you aren't a computer scientist.
>>
>>108440622
Computer scientists are the most autistic retards in existence, so you're doing me a favor here, pal.
>>
>>108436418
If it needs optimization to be good, and we have optimization, then it can be good. You really haven't refuted much here and a little understanding of logic would reveal that.
>>
>>108440766
>we have optimization
We have, like, the barest baselines of optimization, which recursion doesn't cover. Anyone with baseline competence in assembly reading could've told you that.
>>
>>108440622
The point is that a recursive solution is sometimes easier, especially when working with recursive data structures like trees, backtracking solutions and such

Let's say you want to the depth of a tree. The recursive solution in it's core is pretty simple
 
max-depth(tree t):
return 1 + max(max-depth(t->left), max-depth(t->right))


Then you just deal with stopping criterion and call it a day, while the imperative solution is not as easy nor expressive
>>
>>108436365
>If recursion is so good
It's not. Clang-tidy flags it and tells you not to do it.
>>
>>108440815
Why would competence in assembly tell you anything about compiler architecture? Like, “I did mov edi eax and now I know about polyhedral program analysis”.
>>
File: constprop.png (11 KB, 900x280)
11 KB
11 KB PNG
>>108440857
I didn't say anything about compiler architecture. This is about compiler outputs, and again, compilers fucking suck at absolutely everything. In a serious society compiler developers would kill themselves in batches.
>>
>>108440893
go ahead. write a better compiler.
>>
>>108440893
Yea bro everyone else is like, le stupid, and you are the only smart person. God that shit's embarrassing. How do you not get embarrassed when you act like this?
>>
File: 1737753069062280.jpg (33 KB, 303x298)
33 KB
33 KB JPG
>>108436418
lol'd
>>
>>108440893
holy dunning kruger
>>
>>108440917
I have. It's called brain.exe, and no one can beat it.

>>108440918
>you are the only smart person
Damn right.
>>
>>108440947
>no one can beat it
kek he thinks he can beat the compiler
>>
>>108440972
Already have, nigger with a hard r.
>>
>>108440980
It's entirely reasonable to assume this is intentional on the part of compiler developers to produce a more robust output. If you genuinely think this behaviour is incorrect, file a bug report and ask the developers why it happens. They are clearly performing the optimisation on `foo` so it not happening on `bar` is likely intentional. I think you are more interesting in feeling intelligent and writing massive whining posts on 4chan than you are in actual software development.
>>
>>108441152
>It's entirely reasonable
Stopped reading there, autist.
>>
>>108441187
You've stopped reading many things.
>>
>>108436365
It's not good
>>
>>108441230
You sound like the author of many things I stopped reading.
>>
>>108436365
You got this backwards. Computers are bad at a lot of things, like real numbers. You wouldn't imply real numbers are bad, would you.
>>
>>108436581
When you implement an algorithm from a paper that uses recursion.
>>
>>108440560
The new grad market is unimaginably terrible (in the US at least). If you go for it, you need to fucking love CS and do internships before finishing your degree, and maybe some extracurriculars. Also, you need to network.
>>
File: 1764917012463117.png (214 KB, 548x407)
214 KB
214 KB PNG
>>108436365
we just need better compilers anon
>>
>>108436410
the problem is that the way recursive functions are easy to reason about and understand gets all scrambled up when written as tail recursive which is honestly not much better than a loop save for keeping things immutable.

I'll keep dreaming and hoping for a compiler that can guarantee every recursive function will be compiled down to a loop.
>>
>>108436845
f(2)
2 >= 1 ? Yes, so return 2 * f(2)
2 * ( 2 >= 1 ? Yes, so return 2 * f(2) )
2 * 2 * ( 2 >= 1 ? Yes, so return 2 * f(2) )
...

Okay, what was supposed to happen here instead of blowing up in your face?
>>
REAL TCO has not been tried yet, the iterative-shills are holding us back!
>>
>>108436410
>>108436418
Yes, if the optimization flag I pass into my compiler is the difference between my program working correctly vs. stack overflowing, then it’s a garbage feature.
>>
>>108436365
Recursion is pretty shit, calling a function recursively is one of the most inefficient things a CPU can do. The best thing a compiler can do to a recursive implementation is optimize it into an iterative one, or at least use its own stack and skip all the calling, returning and calling convention shit.

If it doesn't do that, then god help you.
>>
>>108443266
loop-shills**
tail call recursion results in an "iterative" process too.
>>
>>108443520
the point of recursion is that most of the time functions become way easier to write and reason about.
you still always need to turn it into a tail recursion or an equivalent loop in cases where performance is better iteratively or you don't know the maximum "n" you'll deal with.

I hope functional and recursion zealots into languages and compilers make us a compiler that can do that on its own in my life time.
>>
>>108443752
Won't happen. Ever.



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

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.