[PEP202 listcomps] (was RE: [Python-Dev] Product iteration)

Tim Peters tim_one@email.msn.com
Tue, 25 Jul 2000 18:57:44 -0400


[Eric S. Raymond]
> ...
> The right way to say
>
> [x**2 for x in range(1, 99) if isprime(x)]
>
> is like this:
>
> [x**2 for x in filter(isprime, range(1, 99)]
> ...
> If you notice that this looks a lot like LISP or F or Haskell, you win.

Win some, lose some:  the original looks much more like Haskell, from which
Greg Ewing borrowed list comprehensions:

    [x**2, x <- [1..98], isprime(x)]

(Note:  that's from memory, as I don't have Haskell on my laptop; while a
 detail may be wrong (in particular, I don't recall how Haskell spells
 "square"), the thrust is not:  Haskell allows an arbitrary sequence of
 iterators and predicates, exactly as does the proposal.  It's
 formally defined in terms of expansion to masses of lambdas and
 applications, but the whole point to the Haskell spelling is to raise
 the expressiveness above the LISP level]

> These languages got comprehensions right on the basis of a lot of
> experience in depth.  We can learn from their example.

Indeed, we're trying to <wink>.  This is much more a Haskell than a LISP
feature, and follows Haskell in using special syntax to relieve the monotony
of relentlessly uniform function composition.  It derives in turn from SETL,
which followed common set-theoretic notation.  This "grabbed" Guido; LISP
spellings never did, and I bet never will.  Indeed, your latter spelling is
so close to the current

    map(lambda x: x**2, filter(isprime, range(1, 99))

as to make little difference.