|
From: <pl...@pi...> - 2012-05-04 04:40:20
|
On 04/05/12 00:26, Tait wrote:
>
>>>> 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.
>
>
I think the C behaviour is the most predictable and clearly defined:
for [ initialisation ; end_condition ; loop_operation ]
it is executed in that order, ie end_condition is tested immediately
after initialisation
note loop_operation can be any expression : i=exp(i*i/sigma)
If there is to be the option of omitting loop_operation , I think it
has to be the unique increment i++ , not an operation that depends upon
the values of the data.
for [ range ] ; is a different thing entirely.
If that is required , I think it should be made quite distinct. probably
with a new data type for ranges. Trying to shoehorn this in as a special
case will lead to ambiguities and language problems later as gnuplot
syntax develops into a more structured language.
Peter.
|