functional programming

Christian Tismer tismer at tismer.com
Sun Feb 27 10:50:56 EST 2000


Moshe Zadka wrote:
> 
> On Fri, 25 Feb 2000, Toby Dickenson wrote:
> 
> > It looks like it should be possible to implement a tail-optimised
> > variant of apply() using Stackless?
> 
> It won't help: the apply call itself won't be tail-optimised....

Why not? I can easily implement an explicit tail call to the
current function. This no automatic tail optimization,
but the programmer decides by hand the he wants to
do a tail jump now.
I could use apply style, or simple function style.

Sketch:

import continuation

def something(arg1, arg2):
    ....
    continuation.tailcall_self(arg1+2, arg2-42)
    # we never get here

# and as we are at it, we can also have the regular call
# of the current function without having to know its name:

def somethingelse(arg1, arg2):
    ....
    continuation.call_self(arg1+2, arg2-42)
    # we do get here

# and advantage: We can now use recursive functions
# in a local context:

def localrecfunc(x1, x2):
    def localfunc(a, b, call=continuation.call_self):
        ...
        if <some condition>:
            return call(a+1, b-2)
            # will come back here
    thing = localfunc(x1, x2)

def localtailfunc(x1, x2):
    def localfunc(a, b, tailcall=continuation.tailcall_self):
        ...
        if <some condition>:
            return tailcall(a+1, b-2)
            # will not come back
    thing = localfunc(x1, x2)


It is easy to do this, and an apply would be easy as well.
I'm just curious about the interface: It is hard to find
the right names for these functions.
Also, they don't appear to belong to the continuation
module, so I hesitate to embed them there and also hesitate
to write another extension module.

Stackless Python can stand all kinds of jumps and calls,
anywhere, at any time. The problem is just how to spell it.

Suggestions?

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list