Possibly Pythonic Tail Call Optimization (TCO/TRE)
antoon.pardon at rece.vub.ac.be
Mon Jul 13 16:42:42 CEST 2015
On 07/13/2015 04:05 PM, Chris Angelico wrote:
> On Mon, Jul 13, 2015 at 11:55 PM, Antoon Pardon
> <antoon.pardon at rece.vub.ac.be> wrote:
>> On 07/13/2015 02:34 PM, Chris Angelico wrote:
>>>>> Warping your code around a recursive solution
>>>>> to make it into a perfect tail call usually means making it look a lot
>>>>> less impressive; for instance,
>>>> And sometimes your problem is very easily solved by a number of functions
>>>> that tail call each other but in python you will need to warp your code
>>>> around an iterative solution, in order to avoid the stack limit.
>>> Example, please? In all my Python programming, I've never hit the
>>> stack limit outside of deliberate playing around (or accidental
>>> infinite recursion, which means it's doing its job).
>> I have had to process text coming from a network connection. The data
>> was mosly unstructered text with some lines that could be used as markers
>> to proceed through some kind of state machine that indicated how the
>> following lines needed to be processed. That was easiest written by having
>> a function for each "state" that would process the next lines until
>> a marker line was encountered and then call the function corresponding
>> with the next state. However since a connection could stay active for
>> days, sooner or later a stack limit would occur if done the natural
>> recursive way.
> I'm not sure why the transition to another state has to be recursive.
Off course it doesn't has to be recursive. It was just easier/more
natural to do in a recursive way and needed warping to do in an interactive
> Maybe this is something where previous experience makes you more
> comfortable with a particular style, which will mean that functional
> idioms will never feel more comfortable to me than iterative ones do,
> and vice versa for you.
Don't give me that. I have generally no problem solving things in
an iterative way. This problem however was most naturally solved
with a functional approach and I had to warp it in order to fit
an iterative approach.
> If that's the case, then I'll stick with
> Python and Pike, and you can happily use Lisp and Haskell, and neither
> of us need be a problem to the other. But honestly, I'm not seeing
> anything that requires infinite recursion in your description. And
> I've worked with a lot of networking protocols in my time.
Your moving the goal posts. I only stated that sometimes the problem
is easily solved by a number of fuctions that tail call each other.
That you can warp this into an interative process and so don't
require infinite recursion doesn't contradict that.
>>>> It seems warping your code is only seen as a problem when going in the
>>>> functional direction. Going into the iterative direction may take all
>>>> the warping that is needed, without comment.
>>> That's because I've never seen code that warps more for iteration than
>>> it does for programming in general. Again, example please?
>> And since when is your experience the measure stick for what other people
>> encounter as problems?
> That's why I said "example please?". My experience has not a single
> time come across this. If you want to make an assertion that iterative
> code requires equivalent warping to tail-recursive code, I want to see
> an example of it. Is that difficult?
Oh come on. It was rather obvious that your request for an example was
not meant to learn something new but was meant for you to have a target
to demolish and thus that you would try to mold that target into something
you were familiar with. Which is exactly what you did above.
More information about the Python-list