News for hackers. Stuff that matters.
Re:What if you couldn’t program with loops? by pcstl on Monday July 15, 2019 @11:31AM (#20441758)
It's the opposite, actually - every loop can be trivially encoded as recursion, but loops can't encode many common recursive patterns without a lot of work.

Recursion also has the potential to be much clearer than loops in some cases.

Of course, there are also cases where one would do much better to not use recursion, but that's why I think people should know of it.

Re:What if you couldn’t program with loops? by BubRoss on Monday July 15, 2019 @12:25PM (#20442332)
Recursion just gives you a stack data structure by letting you use the call stack. If you have a stack data structure to use explicitly, then recursion doesn't give you anything and takes away the simplicity of being able to see the entire state of the traversal easily since the call stack and traversal state are now intertwined.

You can just take the variables you are passing to each recursive call, put them in a struct, and make a stack of those structs.

Re:What if you couldn’t program with loops? by pcstl on Monday July 15, 2019 @02:19PM (#20443597)
Sure, but depending on the recursion, that technique will vary from "inelegant" to "maddeningly brittle".

It seems like going pretty far just to avoid recursion, unless you're in a resource-constrained domain.

Re:What if you couldn’t program with loops? by BubRoss on Monday July 15, 2019 @11:13PM (#20447215)
There is nothing brittle about it, you push values to the stack instead of passing them to a function (which just pushes them to the call stack). It actually ends up being far less brittle in practice because it is much easier to deal with edge cases and errors. You can break out of a loop much easier than returning different values and having those bubble up.