Arithmetic sequences in Python

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Jan 16 11:52:54 CET 2006


On Mon, 16 Jan 2006 01:01:39 -0800, Gregory Petrosyan wrote:

> Please visit http://www.python.org/peps/pep-0204.html first.
> 
> As you can see, PEP 204 was rejected, mostly because of not-so-obvious
> syntax. But IMO the idea behind this pep is very nice. So, maybe
> there's a reason to adopt slightly modified Haskell's syntax? Something
> like
> 
> [1,3..10]  -->  [1,3,5,7,9]

-1 on the introduction of new syntax. Any new syntax.

(I reserve the right to change my mind if somebody comes up with syntax
that I like, but in general, I'm *very* negative on adding to Python's
clean syntax.)

For finite sequences, your proposal adds nothing new to existing
solutions like range and xrange. The only added feature this proposal
introduces is infinite iterators, and they aren't particularly hard to
make:

def arithmetic_sequence(start, step=1):
    yield start
    while 1:
        start += step
        yield start

The equivalent generator for a geometric sequence is left as an exercise
for the reader.

If your proposal included support for ranges of characters, I'd be more
interested.



-- 
Steven.




More information about the Python-list mailing list