Something is rotten in Denmark...

rusi rustompmody at gmail.com
Sun Jun 5 08:26:33 EDT 2011


On Jun 5, 5:03 pm, Jussi Piitulainen <jpiit... at ling.helsinki.fi>
wrote:
> rusi writes:
> > On Jun 3, 11:17 am, Jussi Piitulainen wrote:
> > > rusi writes:
> > > > So I tried:
> > > > Recast the comprehension as a map
> > > > Rewrite the map into a fmap (functionalmap) to create new bindings
>
> > > > def fmap(f,lst):
> > > >     if not lst: return []
> > > >     return [f(lst[0])] + fmap(f, lst[1:])
>
> > > > Still the same effects.
>
> > > > Obviously I am changing it at the wrong place...
>
> > >    >>> fs = [(lambda n : n + i) for i in range(10)]
> > >    >>> [f(1) for f in fs]
> > >    [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>
> > >    >>> fs = list(map(lambda i : lambda n : n + i, range(10)))
> > >    >>> list(map(lambda f : f(1), fs))
> > >    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
> > Thanks Jussi for that code -- but I am not fully able to wrap my head
> > around it.
>
> Oops, sorry, I seem to have edited out a question that I meant to ask.
> The question was this: How do -you- write the list comprehension in
> terms of your fmap? What -is- the expression that still has the same
> effects? That is, where do you bind the i's?
>
> The obvious-to-me way is shown above, but there it is the outer lambda
> that establishes the distinct i's for the different closures.
>
> (The composition list(map(...)) works in both versions of Python.)
>
> > Is the problem in the lambda? ZF?
> > Are you trying to say that map works (functionally) and ZF is
> > imperative?
>
> Sorry, what is ZF?
>
> I'm saying that your fmap works, but in itself it does not provide the
> bindings that we are talking about, and you didn't show what does. The
> outer lambda in my example does that.
>
> The Python list comprehension [... for i in ...] binds (or assigns to)
> just one i which is shared by all the closures above. They end up
> having the same value for i because it's the same i.
>
> I hope this is less obscure now.

I was wondering why the list(...
Now I see that map returns normal lists in python2 and some generator-
like-thing in 3
I would have said: Shall we just stick to 2 (for this discussion) but
then 2 seems to have a double error that self-corrects this example...

OOOOFFF -- too many variables...

[ZF is Zermelo-Fraenkel -- old name for list comprehension  -- I guess
my age leaks like python's ZF (sorry comprehension) :-) ]

Anyway... My (summary,tentative) conclusion is that python's
comprehensions leak.
The one leak fixed in python3 has not fixed the other (revealed in
this thread)

All this has little to do with lambda (whose scope rules were fixed
around python 2.2 IIRC)

I'd be interested in ur take on this...



More information about the Python-list mailing list