[Python-Dev] re: list comprehension / pop quiz

Jeremy Hylton jeremy@beopen.com
Tue, 11 Jul 2000 16:20:36 -0400 (EDT)


>>>>> "GvR" == Guido van Rossum <guido@beopen.com> writes:

  GvR> I'm afraid the answer will be the same, if these are again
  GvR> numerical analysts -- these people live by nested DO loops. :-(

If we end up calling this new feature list comprehensions, we might
create the expectation that they behave like list comprehensions in
other languages.

Haskell is one language with comprehensions and they behave the way
that the numerical analysts expect them to behave.  Maybe we should
chalk it up to numerical analysts and funtional programmers being
weird, but they may be on to something.

Here's a short excerpt from the Gentle Introduction to Haskell:
http://www.haskell.org/tutorial/goodies.html (Sec. 2.4.1)

>[ f x | x <- xs ]
>This expression can intuitively be read as "the list of all f x such
>that x is drawn from xs." The similarity to set notation is not a
>coincidence. The phrase x <- xs is called a generator, of which more
>than one is allowed, as in:
>
>[ (x,y) | x <- xs, y <- ys ]
>
>This list comprehension forms the cartesian product of the two lists
>xs and ys. The elements are selected as if the generators were
>"nested" from left to right (with the rightmost generator varying
>fastest); thus, if xs is [1,2] and ys is [3,4], the result is
>[(1,3),(1,4),(2,3),(2,4)].

Jeremy