List comprehensions

Alexander Williams thantos at chancel.org
Mon Dec 20 22:44:24 EST 1999


On 20 Dec 1999 16:29:24 GMT, Albert Hofkamp <hat at se-46.wpa.wtb.tue.nl>
wrote:

>      print [ (x,y) | x <- xs, x>3, y <- xs, y>x ]

Oooh, Haskell syntax.  I'm feeling perfectly at home.  :)

>I'd like to have that, except that I don't know how to write that
>down. On the other hand, in our field, it is probably not needed much.

I'm not sure that writing parallel iterators as a single line is
really condusive to understanding what is going on under the hood.
You can always nest iterators, as well ...  That's always fun.  :)

>Also, I'd like to get rid of fold().

Hey, I /like/ fold (foldl, foldr)!  After all, the results of fold
aren't lists, they're single values.  It makes sense for it to be a
function.

>For example, how would you write an expression which calculates the sum
>of all elements (or the sum of all elements which hold for some special
>condition ?)

    sumGreaterThanFive(lst) = foldl(operator.plus,
                                    [e <- lst, e>5])

The fact you can pass iteration lists around is high fun.  :)

-- 
Alexander Williams (thantos at gw.total-web.net)           | In the End,
  "Join the secret struggle for the soul of the world." | Oblivion
  Nobilis, a new Kind of RPG                            | Always
  http://www.chancel.org                                | Wins



More information about the Python-list mailing list