[Pharo-users] Interval form: x to: y when x > y - or how to count down...

Richard O'Keefe raoknz at gmail.com
Thu Feb 28 07:38:29 EST 2019

```Of the couple of hundred programming languages I have
used, there is precisely one that does what you expect
(the S programming language, as implemented in R).
And it is a major pain in the posterior with no upside
that I can discern.

Suppose I want a sequence of n consecutive integers
beginning with 1.  In Smalltalk, (1 to: n) does the
job.  In R, 1:n *almost* does the job.  But what
happens when n = 0?  Smalltalk gives me the right
answer: an empty sequence.  R gives me (1,0).  That
means that *every* *flaming* *time* I write
for (i in 1:n) {...}
I have to take special care to ensure that n is not
0, sometimes even having to whack in an extra
if (n > 0) for (i in 1:n) {...}

Trawling through my Smalltalk code, I find that about
6.8% of my counted loops are by: -1,
0.7% of them are by: a negative number other than -1,
2.5% of them are by: a positive number other than 1,
90  % are just to:do: with no by:
Inspecting some of the 90% showed that many of them
would go catastrophically wrong if 1 to: 0 do:
performed its body

On Sat, 23 Feb 2019 at 03:58, Tim Mackinnon <tim at testit.works> wrote:

> I've just been caught out with Intervals - why can't you do:
> 5 to: 1 do: [ :i | Transcript show: i printString]  (eg a negative
> interval)?
>
> if you want to iterate down a series of numbers do you really have to do:
> 5 to: 1 by: -1 do: […
>
> I always assumed that if x > y the step was automatically -1 (but its not).
>
> I asked on Discord - and someone pointed out that if you use variables it
> could be ambiguous - but is it really? I don’t know if other smalltalks do
> it this way (I vaguely recall Dolphin going down, but not sure).
>
> Either way the Interval comment should mention this - and I’ll correct it
> when I get the wisdom of the crowd here.
>
> Tim
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20190301/51ce7cdb/attachment.html>
```