PEP 276 Simple Iterator for ints

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Fri Nov 16 22:03:47 CET 2001


Wed, 14 Nov 2001 11:40:39 -0800, James_Althoff at i2.com <James_Althoff at i2.com> pisze:

> A couple of points: it appears that the Haskell syntax mentioned
> above assumes a closed interval.

Indeed. And it has little choice given that it applies not only to
numbers; ['A'..'Z'] looks better than ['A'..succ 'Z'] and we would
have a bigger trouble when instead of 'Z' we wanted the last value
of the given type - there is no successor to give as the endpoint.

Haskell rarely uses indices anyway, because lists are much more common
than arrays (what Python calls lists is generally called arrays;
Haskell's list are more like Lisp lists). Lists are better processed
by functions like map and zip or by recursion than by indexing. s[i]
is expensive, s[1:] is cheap - it's opposite in Python. That's why
it's not a big problem in the context of Haskell that you need to
subtract 1 from the length to get the endpoint in the range of indices.

But Python uses indices a lot, and it already uses half-open intervals
for ranges. It would be very confusing if some interfaces used
half-open interfals and other used closed ones. To be self-consistent,
Python needs to continue to use half-open intervals.

IMHO this excludes the syntax looking like [i..j] because it suggests
that j belongs to the end. Half-open intervals are indeed more
convenient for working with indices - the only problem is that they
don't have an obvious syntax.

IMHO we should either do nothing, or make iterators for ints as a
shortcut - leaving range and xrange for more general cases, or maybe
arrange that range(len(sequence)) can be spelled range(sequence).

> Also, it appears that this syntax creates an actual list, not
> an iterator.

Haskell's list are lazy (as all data structures), there is no
difference - they act similarly to iterators.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK



More information about the Python-list mailing list