From: Stephen Leake on
Simon Wright <simon(a)pushface.org> writes:

> Stephen Leake <stephen_leake(a)stephe-leake.org> writes:
>
>> "Peter C. Chapin" <pcc482719(a)gmail.com> writes:
>>
>>> procedure Do_Something
>>> (Buffer : in Buffer_Type;
>>> Fst : in Natural;
>>> Lst : out Natural;
>>> Ok : out Boolean);
>>
>> Change this to:
>>
>> procedure Do_Something
>> (Buffer : in Buffer_Type;
>> Last : in out Natural;
>> Ok : out Boolean);
>>
>> -- Operate on Buffer (Last + 1 ..), update Last to last item operated
>> -- on.
>
> What would happen if Buffer'First was Natural'First?

Good point. Last should be Integer, not Natural, so Last can be
initialized to Buffer'first - 1.

Which involves defining the type with an "extra" value, which Peter said
he didn't like. I find it quite useful in situations like this.

--
-- Stephe
From: Colin Paul Gloster on
On Sat, 12 Jun 2010, Peter C. Chapin sent:
|------------------------------------------------------------------------------|
|"[..] |
| |
|I want to invoke this procedure repeatedly on a particular buffer such that |
|each invocation picks up where the previous invocation left off. For example |
| |
|Ok : Boolean := True; |
|Index_Fst : Natural := Buffer'First; |
|Index_Lst : Natural; |
|... |
| |
|while Ok and Index_Fst <= Buffer'Last loop |
| Do_Something(Buffer, Index_Fst, Index_Lst, Ok); |
| Index_Fst := Index_Lst + 1; |
|end loop; |
| |
|The problem with this code is that the assignment to Index_Fst inside the loop|
|might raise Constraint_Error if Index_Lst = Buffer'Last after Do_Something |
|finishes. I can work around this problem but my solutions tend to be rather |
|ungainly looking. Surely there must be an easy way to handle this. |
| |
|Peter" |
|------------------------------------------------------------------------------|

You seemed to dislike the good idea of using different ranges for the
cursors and the array mentioned elsewhere in this thread.

So how about something like
....
Index_Lst : integer := Buffer'First-1; --Hmm, perhaps I have merely
--moved your disliked extra 1.
....

while Ok and Index_Lst + 1 <= Buffer'Last loop
Index_Fst := Index_Lst + 1;
Do_Something(Buffer, Index_Fst, Index_Lst, Ok);
end loop;
?

It is a pity that I have introduced code cloning, but looping without
sticking to predominant forms can have this consequence in any
language I can think of (well, just the languages which have already
been defined, that is, excluding languages we can devise).

I really do not approve of the names Index_Fst and Index_Lst. How many
people reading this have still not noticed Index_Fst is not in this
version's condition? Be candid.

I wish Ada success on student satellites!

Regards,
Colin Paul
From: Simon Wright on
Colin Paul Gloster <Colin_Paul_Gloster(a)ACM.org> writes:

> I really do not approve of the names Index_Fst and Index_Lst.

Quite agree. What's wrong with First and Last? (or even if it wasn't
obvious already Index_First, Index_Last).
From: Peter C. Chapin on
Simon Wright wrote:

> Colin Paul Gloster <Colin_Paul_Gloster(a)ACM.org> writes:
>
>> I really do not approve of the names Index_Fst and Index_Lst.
>
> Quite agree. What's wrong with First and Last? (or even if it wasn't
> obvious already Index_First, Index_Last).

'First' and 'Last' are reserved by SPARK as FDL identifiers. Similarly 'Start'
and 'Finish' are reserved by SPARK. Obviously 'Begin' and 'End' is not going
to work either since they are reserved by Ada. So while I rather
dislike 'Fst' and 'Lst' I found it awkward finding a suitable matched pair of
names that would otherwise convey my intention.

In my actual program the index variables are named differently than in my
example anyway. However, I admit that I have used 'Fst' and 'Lst' at times in
my code since I couldn't think of anything else that wasn't reserved. I
probably lack imagination.

Peter


From: Simon Wright on
"Peter C. Chapin" <pcc482719(a)gmail.com> writes:

> Simon Wright wrote:
>
>> Colin Paul Gloster <Colin_Paul_Gloster(a)ACM.org> writes:
>>
>>> I really do not approve of the names Index_Fst and Index_Lst.
>>
>> Quite agree. What's wrong with First and Last? (or even if it wasn't
>> obvious already Index_First, Index_Last).
>
> 'First' and 'Last' are reserved by SPARK as FDL identifiers. Similarly
> 'Start' and 'Finish' are reserved by SPARK. Obviously 'Begin' and
> 'End' is not going to work either since they are reserved by Ada. So
> while I rather dislike 'Fst' and 'Lst' I found it awkward finding a
> suitable matched pair of names that would otherwise convey my
> intention.
>
> In my actual program the index variables are named differently than in
> my example anyway. However, I admit that I have used 'Fst' and 'Lst'
> at times in my code since I couldn't think of anything else that
> wasn't reserved. I probably lack imagination.

Oops, I didn't know that.

A shame that to make code provable you have to make it unreadable.