lainchan / λ

(untitled)

Crystepsi · 2023-02-17 01:30
can someone explain to me in monkey terms why we would use recursion over a loop
bit · 2023-02-17 02:02
Recursive data structures (e.g. trees, linked lists). Using it to iterate on vectors is just retardation from ML "muh state" kiddies.
Agent Smith · 2023-02-17 04:29
>>34368 Recursion accomplishes what a for loop + a stack accomplish
dictionary · 2023-02-17 05:25
>>34368 All of these heckin noob threads, please use the noob thread stickied you animals.
James K · 2023-02-17 05:54
>>34368 It's shorter. define visit_tree_nodes(tree) { var stack = new Stack; stack.push(tree); while !stack.empty() { var current = stack.pop(); visit(current.value); for child in tree.children { stack.push(child); } } } vs. define visit_tree_nodes(tree) { visit(current.value); for child in tree.children { visit_tree_nodes(child); } } Please don't make a thread for something as simple as this in future.
SHODAN · 2023-02-17 07:25
>>34372 >It's shorter. and catastrophic if you don't account for all base cases only use recursion if you absolutely need to
swap · 2023-02-17 08:57
>>34368 >>34369 Much more important for trees than linked lists A linked list can be traversed in a loop, but many forms of tree handling simply can't be done (or become far more difficult than necessary) without recursion
setq · 2023-02-17 09:51
>>34369 >>34368 They also taught me recursion for iterating over arrays in high school, as part of AP CS A - I suspect that's where the confusion might be coming from.
Miho Iwakura · 2023-02-17 10:32
I have ran into points where recursion is cleaner at work and in personal projects but I honestly can’t put into words why, I will say if a loop starts to feel futile for a repeated operation and seems to be getting out of control, try a recursive function it might be what is needed to solve the issue.
James K · 2023-02-17 11:08
>>34373 Recursion is structurally the same as an iterative approach, so it being shorter makes you less likely to do it wrong. As for the "destructive" consequences, I'm not sure overflowing the stack and crashing is much worse than getting stuck in a loop and eventually using up all the system's RAM. The only real risk with a recursive approach is overflow which is typically something you don't need to worry about until you get to a few hundred thousand calls deep. There are also some languages whose run-times completely avoid the issue of overflow by resizing the stack dynamically. This is also all assuming head recursion. Tail calls, when properly optimised, won't overflow the stack and can often be surprisingly expressive when compared to the iterative counterpart.
hash table · 2023-02-17 12:42
>>34375 >Data structure needs recursion No. No single data structure can only be traversed via recursion. That would be like breaking a law of physics.
Gyula Kovacs · 2023-02-17 13:35
>>34383 Depending on what exactly you're doing with it, it's possible to traverse them without recursion, if and only if you create a second data structure that's morally equivalent to recursion, or worse
() · 2023-02-17 14:54
>>34383 I don't understand, a tree or file directory is more suited to recursion? The iterative approach just ends with an explicit stack
Delilah · 2023-02-17 20:44
>>34382 I think I figured out how to put it into words, you have multiple moving targets and multiple base cases and multiple transforming variables within what ever collection you are working with, that’s when recursion actually feels easier and makes sense to use.
Batou · 2023-02-18 06:03
>>34384 >>34385 The man I'm replying to said some data structures require recursion. You both are simply proving my point that it is not true.
Stallman · 2023-02-18 07:04
>>34368 Sometimes using recursion is cleaner. Read Little Schemer. >>34371 >All of these heckin noob threads, please use the noob thread stickied you animals. This! Creating more threads make it actually harder to answer questions. most people check the Q&A threads periodically.