better lambda support in the future?

Mike Meyer mwm at mired.org
Sat Dec 18 03:48:59 CET 2004


Jeff Shannon <jeff at ccvcorp.com> writes:

> Steven Bethard wrote:
> Hm, possibly.  I must confess that my direct knowledge is limited to a
> fairly narrow set of languages, and that C and C++ are the only
> statically-compiled languages I've used.  Still, I'm not sure that
> it's just a matter of functions as first-class objects.  Would OCaml
> (or some other static language) have something that's equivalent to
> this?
>
> def f(x):
>     if x < 0:
>         def g(y):
>             return y * -1
>     else:
>         def g(y):
>             return y
>     return g
>
> foo = f(1)

This is really no different than:

def f(x):
    if x < 0:
       g = -y
    else:
       g = y
    return g

The only difference is that you're binding the variable g to a y type
instead of a function type. That's what "first class functions"
means. You can treat functions like any other object, assign them to
variables, pass them as parameters, and so on.

           <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list