List comprehensions

skaller skaller at maxtal.com.au
Thu Dec 16 12:33:41 EST 1999


What is the proposed syntax for list comprehensions?

I have implemented:

	[ x + y for x in [1,2,3]]

in Viper **. However, this is not very general, because it only allows
a _single_ variable (x in the example). More precisely,
you can have parallel variables like:

	[ x + y for x,y in [(1,2), (2,3), (3,4)]]

but not products, that is, something like:

	[x+y for x in list1 for y in list2]

------------------------------------------------
Actually, a single 'for' is enough, provided you 
like functional combinators like:

	zip -- make a list of pairs from two
		equal length lists
	unzip -- make two lists from a list of pairs

	cross -- create a list of pairs of all elements 
		from both lists
	etc -- other suitable list functors

and their generalisations. Note that zip is already
available  -- zip(a,b) is just map(None, a,b) or something.
But the idea of the syntax is to reduce the need for
functional syntax.

----------------------------------------------------
** Viper is now available for Un*x, the first alpha
can be downloaded for evaluation (ONLY)
from the temporary location

	ftp://ftp.cs.usyd.edu.au/jskaller/viper_2_0_a1.tar.gz

You will need ocaml (caml.inria.fr) built with -pthreads, 
Gtk, readline, and, of course, Python. [There is a also
a prebuilt binary for linux, but it may not run,
since the python location is hard coded at 
/usr/local/lib/python1.5]

-- 
John Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850



More information about the Python-list mailing list