[Python-ideas] Where-statement (Proposal for function expressions)

Piet van Oostrum piet at cs.uu.nl
Thu Jul 16 11:46:19 CEST 2009


>>>>> Gerald Britton <gerald.britton at gmail.com> (GB) wrote:

>GB> foo(x,y) = myfunc(bar) where:
>GB>     myfunc = lambda x:f(x)
>GB>     bar = lambda y:g (y)
>GB>     where:
>GB>         f = lambda x:getattr(x, 'func')
>GB>         g = lambda y:str(y)

>GB> would translate to:

>GB> def foo(x,y):
>GB>     return getattr(x, 'func')str(y)

I don't think so. In the original code x and y after the where are only
used as bound variables in the lambda's so they are not the x and y of
the foo def. You could replace all of them by p and q without changing
the meaning.

If you replace all lambda x: fun(x) by the equivalent fun then you get
this:

def foo(x,y):
    return getattr(str, 'func')

x and y are not used.

>GB> Basically Haskell reads like math:

>GB> circle_area = pi * radius**2 where pi is 3.14159 and radius is the
>GB> radius of the circle

>GB> You unpack it as you go.  You could do the same this way:

>GB> f = lambda x:getattr(x,'func')
>GB> g = lambda y:str(y)

>GB> bar = lambda y: g(y)
>GB> myfunc = lambda x: f(x)

>GB> foo(x,y) = lambda x,y: myfunc(x)(bar(y))

Huh?
How do you come at this step? IMHO myfunc(bar) is not the same as 
lambda x,y: myfunc(x)(bar(y)).
It would be the same as (lambda x: myfunc(x))(lambda y: bar(y))

-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org




More information about the Python-ideas mailing list