News for hackers. Stuff that matters.
Re:Mindset Shifts for Functional Programming by Herval_freire on Wednesday April 05, 2023 @10:42AM (#35455081)
>Nobody has yet convinced me that recursion has any sustained advantage over looping.

Recursion gives you a stack by default. You don't have to explicitly think about the stack. In looping the stack must be explicit.

Recursion and looping are the same thing. Recursion can be mechanically translated to a for loop and a stack, the concepts are isomorphic.

Re:Mindset Shifts for Functional Programming by KRAKRISMOTT on Wednesday April 05, 2023 @11:09AM (#35455516)
On paper perhaps, but I have yet to see a compiler that can take any arbitrary recursive subroutine and automatically optimize it into a Tail Call Optimized (that is, transform the recursive parts into goto/jumps similar to a loop) version. Non optimized recursion is not hard to understand, the problem is any performant recursive code needs to be manually rewritten as tail recursive which adds a lot of complexity. Built a better compiler and maybe more people would be willing to embrace functional programming.
Re:Mindset Shifts for Functional Programming by Herval_freire on Wednesday April 05, 2023 @11:27AM (#35455811)
It doesn't make any sense to convert any arbitrary recursion into tail called optimized.

If the recursion can be tail called optimized then yes the loop is the optimized performant implementation.

But if the recursion fundamentally utilizes the call stack then the reverse is actually true. The recursion is now the performant implementation of a for loop. So a loop conversion optimization actually doesn't make sense here. That's partly why a compiler won't optimize this.

Why? Because in recursion you utilize the call stack, in the for loop you're going to create a heap allocated stack and allocate on it repeatedly. Allocation slows down the iterations.

The only advantage of the for loop in this case is that there won't be stack overflow, but overall the recursive version will actually be faster.

I think the overflow may be part of the reason why the reverse conversion isn't done of taking heap allocated stacks and converting them into call stacks. The other issue is ownership of the heap allocated stacks, you can't convert it to a call stack if other entities own it or something else references it.

Re:Mindset Shifts for Functional Programming by mike_hock on Wednesday April 05, 2023 @01:48PM (#35457896)
Works for O(log n) stack depth. In general, recursion is not viable.
Re:Mindset Shifts for Functional Programming by zelphirkalt on Wednesday April 05, 2023 @02:40PM (#35458653)
This is quite false. Languages like Scheme (dialects), ML variants, Erlang and Haskell all make use of recursion in general and they produce very viable results.
Re:Mindset Shifts for Functional Programming by sokoloff on Wednesday April 05, 2023 @02:28PM (#35458483)
> If the recursion can be tail called optimized then yes the loop is the optimized performant implementation.

Is it? My mental model of tail-call optimization is that it’s just a replacement of the stack variable values in place and a jump to the function entry point (which seems like the same amount of work as updating local variables in a loop and jumping to the head of the loop).

Re:Mindset Shifts for Functional Programming by zelphirkalt on Wednesday April 05, 2023 @02:38PM (#35458625)
> The only advantage of the for loop in this case is that there won't be stack overflow, but overall the recursive version will actually be faster.

In languages, which have realized the value of recursion, a stack overflow does not happen. For example: https://docs.racket-lang.org/guide/Lists__Iteration__and_Rec...

Re:Mindset Shifts for Functional Programming by soegaard on Thursday April 06, 2023 @01:39PM (#35471736)
Re:Mindset Shifts for Functional Programming by illiarian on Wednesday April 05, 2023 @11:24AM (#35455772)
Unless I'm mistaken, Erlang's compiler rewrites all recursion that explicitly returns a function call into tail-call, and eliminates all the stack in between
Re:Mindset Shifts for Functional Programming by timmg on Wednesday April 05, 2023 @01:34PM (#35457674)
> all recursion that explicitly returns a function call

Yeah, but that's not "all recursion".

Re:Mindset Shifts for Functional Programming by chriswarbo on Wednesday April 05, 2023 @01:27PM (#35457581)
> On paper perhaps, but I have yet to see a compiler that can take any arbitrary recursive subroutine and automatically optimize it into a Tail Call Optimized (that is, transform the recursive parts into goto/jumps similar to a loop) version.

General recursion can be modelled coinductively via the "Delay" or "Partial" type, e.g. https://arxiv.org/abs/cs/0505037

Type-inference can figure out when recursive calls need to be wrapped in `Later`. We can then compile into a single `while` loop to unwrap the layers.