List Comprehensions Enhancement

Phil Hunt philh at vision25.demon.co.uk
Tue Sep 14 05:06:23 EDT 1999


In article <7riudv$24g$1 at c3po.schlund.de>
           hannah at schlund.de "Hannah Schroeter" writes:
> In article <37D9E932.66F2B0D3 at cosc.canterbury.ac.nz>,
> Greg Ewing  <greg at cosc.canterbury.ac.nz> wrote:
> >[...]
> >print [3 * x for x in nums]
> >print [x for x in nums if x > 2]
> >print [(i, s) for i in nums for s in strs]
> >print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
> 
> The syntax looks a bit verbose, compared to e.g. Haskell's list
> comprehension syntax which looks quite similar to the mathematical set
> comprehensions:
> 
> print [3 * x | x <- nums]
> print [x | x <- nums, x > 2]
> print [(i,s) | i <- nums, s <- strs]
> print [(i,s) | i <- nums, s <- [f | f <- strs, 'n' `elem` f]]
> 
> Also, I think there should be a clear separator between the
> generators/predicates in your syntax too:
> 
> print [(i,s) for i in nums, for s in strs]

I agree, here.
 
> or similar.

Or use a Smalltalk-like collect reserved word, e.g.:

print [collect 3 * x for x in nums]
print [collect (i,s) for i in nums, for s in strs]


-- 
Phil Hunt - - - - - - - - -  philh at vision25.demon.co.uk
 - Linux will be 8 years old on 17th September! See: -
http://www.vision25.demon.co.uk/prog/linuxbirthday.html





More information about the Python-list mailing list