number ranges
Paul Rubin
http
Tue Feb 21 16:07:10 EST 2006
aleaxit at yahoo.com (Alex Martelli) writes:
> > for i in (1 to 10 by 3):
> > print i
> >
> > should print 1 4 7.
>
> But that would be an "attractive nuisance" to many other native speakers
> like you, who would instinctively think of a closed interval. Maybe
> 'upto' rather than 'to', as somebody else suggested, would ease that.
I don't think "upto" is any better. Any distinction that might exist
between "to" and "upto" doesn't jump out at me.
I notice that Haskell uses closed intervals: [1..5] means [1,2,3,4,5].
Is that an error by Haskell's designers? One possibility is that those
intervals don't actually get used in ways likely to cause one-off errors.
I haven't used Haskell enough to have any sense of this.
Here's something ugly but explicit, somewhat Haskell-inspired:
(1 .. ) # infinite generator, like Haskell
(1 .. < 10) # semi-closed interval: 1,2,3...,9
(1 .. <= 10) # closed interval: 1,2,3...,10
(1,4 .. < 10) # semi-closed: 1,4,7
(1,4, .. <= 10) # closed: 1,4,7,10
This would be horrible for Python though.
More information about the Python-list
mailing list