News for hackers. Stuff that matters.
Re:PyTorch: Where we are headed and why it looks a lot like ... by creata on Friday November 26, 2021 @08:23PM (#29356216)
Tail calls and while loops are essentially equivalent, so why care whether a language prefers one or the other?
Re:PyTorch: Where we are headed and why it looks a lot like ... by jhgb on Saturday November 27, 2021 @12:47AM (#29357818)
They're not "essentially equivalent". A while loop can't begin in one module and end in another. A tail call sequence can. Loops are not modular.
Re:PyTorch: Where we are headed and why it looks a lot like ... by creata on Saturday November 27, 2021 @12:57AM (#29357857)
Just to be clear, are you suggesting mutual recursion across modules?
Re:PyTorch: Where we are headed and why it looks a lot like ... by jhgb on Saturday November 27, 2021 @10:06AM (#29360685)
Just to be clear, I'm using the word "module" here in an extremely generic, language independent way. Basically in the sense of "things that can be compiled individually". In many languages (for example in Lisp), those are functions. A syntactic loop construct ordinarily can't even cross a function boundary.

If your language of choice features formalized modules in the Modula-2 sense, its loops most likely can't span multiple functions even within a single Modula-2-sense module, even if you wanted that (for example, in a state machine with named states as functions which tail-call each other to switch state by transferring control, or something like that).

Re:PyTorch: Where we are headed and why it looks a lot like ... by krastanov on Saturday November 27, 2021 @07:22AM (#29359625)
Why is that unreasonable? I might be missing something (I have no formal CS education), but I can imagine it, e.g. a base graph datastructure library, and then two libraries dependent on the base library being used together for some graph traversal work (where callbacks are used).
Re:PyTorch: Where we are headed and why it looks a lot like ... by Vetch on Friday November 26, 2021 @09:57PM (#29356895)
It's like, why use one synonym and not the other? Sometimes it's because writing things in an immutable manner makes things clearer. Other code that can benefit are certain coroutines and generators.
Re:PyTorch: Where we are headed and why it looks a lot like ... by creata on Friday November 26, 2021 @10:08PM (#29356970)
> It's like, why use one synonym and not the other? Sometimes it's because writing things in an immutable manner makes things clearer.

That's true in many situations, but in this case the equivalence between the two is so straightforward that I can't see recursion gaining you any clarity. Perhaps you have a concrete example?

Re:PyTorch: Where we are headed and why it looks a lot like ... by Vetch on Saturday November 27, 2021 @10:02AM (#29360649)
I can't think of a specific example but I do know that there have been times where I have chosen to write a loop as tail recursion and times where I have used mutable variables and a while loop. Why one or the other might be recursion better capturing intent, preferring immutability, efficiency or just a matter of taste.

Sometimes it makes more sense and is more flexible to package functionality as a recursive function, then mutation and variables feel like clutter. Other sometimes using recursion instead of a loop gains nothing expressively. Or sometimes structural recursion + pattern matching is the much more elegant approach in terms of communicating and capturing the essence of the algorithm.

Re:PyTorch: Where we are headed and why it looks a lot like ... by gleenn on Friday November 26, 2021 @08:48PM (#29356417)
Because if you like functional programming, then avoiding loops and imperative code is desirable. Some of us hate seeing `for` and `while` because they also imply mutation of variables. Recursion is beautiful.