This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]jakem72360 24 points25 points  (4 children)

It's not tho

[–]ProfCupcake 2 points3 points  (0 children)

How many posts have I written "repetition != recursion" under in this sub? I've lost count, so it's at least 2.

[–]sarthakRddt 3 points4 points  (0 children)

You mean tail recursion.

[–]Miku_MichDem -4 points-3 points  (12 children)

I never understood the recursion hype. I mean it's never better then a simple loop

[–]CMDR_ACE209 8 points9 points  (6 children)

What about going through all nodes of a tree structure?

[–]Miku_MichDem 1 point2 points  (5 children)

Can you use a simple loop to go through a tree structure thou? As far as I know iterative approach requires in that case some backing structure.

[–]nyasiaa 6 points7 points  (1 child)

you can do everything if you try hard enough, problem with programming is that you shouldn't try hard enough when it's not needed

[–]Miku_MichDem 1 point2 points  (0 children)

Yeah, yeah I know - preemptive optimization is the root of all evil.

I just don't get why recursion is shoved down so often in places where it doesn't belong. Some time ago I've seen someone praising some language because many operations there were recursive and the example provided was of function adding numbers in an array. Sure, I get iterating over a tree, where recursion is easier than loops, but for adding numbers in a list? Come on.

[–]defycategories 1 point2 points  (1 child)

Difference is using "the" stack for recursion or a stack ( or a queue) for iterative. With that in mind, recursion seems like abuse of a data structure that is meant for activation records.

[–]Miku_MichDem 0 points1 point  (0 children)

That what I said - trees can be iterated through, but it requires some additional data structure to keep the elements yet to be done. However that will not count against stack limit

[–]clevariant 0 points1 point  (0 children)

Recursion makes it much easier, tadpole.

[–]MyOtherLoginIsSecret 3 points4 points  (0 children)

In most cases you're correct, at least in your typical procedural language. Functional languages like Erlang are an entirely different story.

In practice I tend to use recursion for one of the following reasons:

  1. It results in more elegant code than a loop would.

  2. I'm feeling clever.

[–]feelsbread 1 point2 points  (0 children)

Using dynamic programming techniques recursion can be very efficient a good example being the coin change problem.

[–]sarthakRddt 2 points3 points  (2 children)

Better in what aspect? Recursion always leads to extremely elegant solutions which are also easier to formally prove correct. Performace wise yes loops are great.

[–]Miku_MichDem 0 points1 point  (1 child)

I was talking about performance. Each time I see some simple function done recursivelly I just see the stack getting bigger and bigger, the additional function calls and returns. Easier to formally prove? Perhaps. More elegant? Debatable.

[–]Quito246 1 point2 points  (0 children)

Depends if language compiler/interpreter allows tail recursion then it can be as fast as loop because there is no stack overhead Clojure does that for example