|
From: Tait <gnu...@t4...> - 2012-05-03 22:26:42
|
> > > So yes, there is an inconsistency. There are currently three "for"
> > > constructs
> > > do for
> > > set for
> > > plot for
> > >
> > > The first two of these always iterate at least once; the third one does not.
> > > The documentation is silent on the intended behavior.
> > > So which to change, "plot for" or both the others?
> >
> > Given that it's called "for" I'd say the principle of least surprise
> > dictate that it act consistently with "for" as used in most programming
> > languages (e.g. C).
Ethan:
>
> It turns out to be surprisingly hard to determine what
> "most programming languages" do. Many require an explicit increment
> operation, like C.
>
> FWIW, here is what you get in R, whose syntax is close to that of gnuplot.
>
> R> for (i in 4:1) { print(i) }
> [1] 4
> [1] 3
> [1] 2
> [1] 1
>
> So that's a third option to consider.
Arun:
> My preferred choice would be that you need to specify the -1 increment
> explicitly.
>
> If you leave it with an increment of +1 unless otherwise specified, I
> also would prefer if "do for [i=5:4] {" does no iterations at all.
Peter:
> I would advise against such "special" cases. It's confusing (unexpected)
> and as I said before makes producing predictable output complicated.
I was thinking in terms of for [a:b] implicitly meaning for [a:b:+1], in
which case there should be zero loops executed if a > b. For example...
Perl:
perl -e "print for 0..9" => 0123456789
perl -e "print for 9..0" =>
Python:
python -c "print range(0,9) => [0, 1, 2, 3, 4, 5, 6, 7, 8]
python -c "print range(9,0) => []
Ruby:
ruby -le 'print (0..9).to_a' => 0123456789
ruby -le 'print (9..0).to_a' =>
But also R, as you mentioned, and...
PHP:
php -r 'print implode(range(0,9))' => 0123456789
php -r 'print implode(range(9,0))' => 9876543210
(...not that PHP should be a model for anything, in my personal opinion)
I do like and have often wished for R's behavior where [a:b] implies a
+1 increment if b>a and a -1 increment if a>b. I think gnuplot could
adopt this. But to address Arun and Peter's concern, for [i=9:0:1]
should do zero loops, to allow a mechansim for the author to say, "I
want Perl/Python/Ruby-like behavior, not R-like behavior." Having
for [i=9:0:1] do one loop can only be seen as broken.
|