* loop step function
@ 1997-01-24 0:00 Paul Van Bellinghen
1997-01-25 0:00 ` Larry Kilgallen
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Paul Van Bellinghen @ 1997-01-24 0:00 UTC (permalink / raw)
I'm fairly new to ADA. I have coded in PASCAL, FORTRAN, C, and assembly
and have found ADA to be similar to PASCAL but with stronger type
checking and with better organization and data hiding. Anyway, there
seems to be an obvious omission in the ADA language. The FOR loop does
not have a "STEP" option. In order to perform a loop function using a
variable's range of values but in steps greater than the variable
kernel, you seem to have to use a WHILE loop with the variable
incremented within the loop by the step size. Is there a more elegant
way of doing this?
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-24 0:00 loop step function Paul Van Bellinghen
@ 1997-01-25 0:00 ` Larry Kilgallen
1997-01-29 0:00 ` Ted Dennison
1997-01-25 0:00 ` Matthew Heaney
1997-01-25 0:00 ` Geert Bosch
2 siblings, 1 reply; 8+ messages in thread
From: Larry Kilgallen @ 1997-01-25 0:00 UTC (permalink / raw)
In article <32E93C0B.36A@cloud9.net>, Paul Van Bellinghen <pvanbell@cloud9.net> writes:
> I'm fairly new to ADA. I have coded in PASCAL, FORTRAN, C, and assembly
> and have found ADA to be similar to PASCAL but with stronger type
> checking and with better organization and data hiding. Anyway, there
> seems to be an obvious omission in the ADA language. The FOR loop does
> not have a "STEP" option. In order to perform a loop function using a
> variable's range of values but in steps greater than the variable
> kernel, you seem to have to use a WHILE loop with the variable
> incremented within the loop by the step size. Is there a more elegant
> way of doing this?
In order to shed the newcomer image, you have to capitalize it as Ada
with this crowd. Pretty silly for a language which is case-insensitive,
but everything else is much more logical.
As regards FOR loops, you could say:
for BY_ONE from 1 to 20 loop
declare
USEFUL_VALUE : INTEGER := BY_ONE*2;
begin
my_procedure ( USEFUL_VALUE );
end;
end loop;
Whether that is "elegant" is certainly debatable, but it is "flexible"
in that:
USEFUL_VALUE : INTEGER := BY_ONE*2;
could be replaced with:
USEFUL_VALUE : INTEGER := (BY_ONE**2)/13;
Larry Kilgallen
The above statements represent my "gut feel" and have not been
authorized by any compiler.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-24 0:00 loop step function Paul Van Bellinghen
1997-01-25 0:00 ` Larry Kilgallen
@ 1997-01-25 0:00 ` Matthew Heaney
1997-01-27 0:00 ` johnherro
1997-01-25 0:00 ` Geert Bosch
2 siblings, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 1997-01-25 0:00 UTC (permalink / raw)
In article <32E93C0B.36A@cloud9.net>, pvanbell@cloud9.net wrote:
>The FOR loop does
>not have a "STEP" option. In order to perform a loop function using a
>variable's range of values but in steps greater than the variable
>kernel, you seem to have to use a WHILE loop with the variable
>incremented within the loop by the step size. Is there a more elegant
>way of doing this?
This is a *deliberate* omission in the langauge.
Here is a clue about why Ada doesn't have a step option:
Programming is a Human Activity
Ada was engineered for use by humans, who tend to make mistakes. It was
designed to omit features in other languages that tended to be error prone.
Ada was designed for use by human programmers
Empirical studies had been done to determine what mistakes were common
among human programmers, and one of the mistakes that programmers always
make is getting loop termination correct.
Figuring out what the last value is during iteration is often difficult and
error prone. Ada solves the problem by only allowing a step size of one,
thus removing the difficulty and removing the source of error.
Just think of Ada's for loop as meaning "this states directly how many
times I iterate." And that quantity isn't necessarily the same as the
number I use in the algorithm.
So Ada's for loop is the no-brainer way to iterate across the values of a
discrete type.
As Larry pointed out in another post, if you need some other value (that
you would use a for-step in another language), then use a local declare
block to calculate the value.
I've been programming in Ada for 10 years, and I can't remember even one
time needing a value other than one as my step value.
If I were going to design a glove for use by humans, then I would design it
to fit the shape of the human hand. If I were going to design a
programming language for use by humans, then I would design it to fit the
cognitive capacities of the human brain.
That language is Ada.
--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-24 0:00 loop step function Paul Van Bellinghen
1997-01-25 0:00 ` Larry Kilgallen
1997-01-25 0:00 ` Matthew Heaney
@ 1997-01-25 0:00 ` Geert Bosch
2 siblings, 0 replies; 8+ messages in thread
From: Geert Bosch @ 1997-01-25 0:00 UTC (permalink / raw)
Paul Van Bellinghen (pvanbell@cloud9.net) wrote:
"Anyway, there seems to be an obvious omission in the ADA language.
The FOR loop does not have a "STEP" option. In order to perform a
loop function using a variable's range of values but in steps
greater than the variable kernel, you seem to have to use a WHILE
loop with the variable incremented within the loop by the step size.
Is there a more elegant way of doing this?"
It is not possible to answer questions like "I used feature X in
language Y, where is X in Ada?" in general. It is very helpful if
you post a small example problem where you don't know how to solve it
using Ada. Line-by-line translations to Ada of your Pascal programs typically
won't give very good results, since it is often necessary to look at the
problem from a slightly different perspective. This is the hardest
part in learning the language.
When you post an example, there is more context to provide you with
an elegant Ada solution.
Regards,
Geert
--
E-Mail: geert@sun3.iaf.nl
``I think there is a world market for maybe five computers.''
Thomas Watson, chairman of IBM, 1943
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-27 0:00 ` johnherro
@ 1997-01-27 0:00 ` Michael Feldman
0 siblings, 0 replies; 8+ messages in thread
From: Michael Feldman @ 1997-01-27 0:00 UTC (permalink / raw)
In article <19970127125200.HAA26735@ladder01.news.aol.com>,
<johnherro@aol.com> wrote:
[snip]
>type CLA_Message_Type is (Ask_For _Help, Give_Help, Discuss_Ada,
>Please_Do_My_Homework, Flame, Flame_The_Flamer);
>
>Now in Ada you could write
>
>for L in Ask_For_Help .. Flame loop
or even (more to the point)
for L in CLA_Message_Type loop
or even
for L in reverse CLA_Message_Type loop
so if the type CLA_Message_Type changes in maintenance, your loop
code doesn't change a bit.
I think this makes up for the lack of a step size. Just my two cents.:-)
Mike Feldman
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-25 0:00 ` Matthew Heaney
@ 1997-01-27 0:00 ` johnherro
1997-01-27 0:00 ` Michael Feldman
0 siblings, 1 reply; 8+ messages in thread
From: johnherro @ 1997-01-27 0:00 UTC (permalink / raw)
Perhaps there's also another reason that a STEP clause was deliberately
omitted from Ada. In many languages, the index of the FOR loop has to be
a numeric (integer in most languages, but in Basic it may also be a
float). However, in Ada the index could be any discrete type, such as an
enumeration type. It doesn't have to be numeric. Here's my favorite
emumeration type:
type CLA_Message_Type is (Ask_For _Help, Give_Help, Discuss_Ada,
Please_Do_My_Homework, Flame, Flame_The_Flamer);
Now in Ada you could write
for L in Ask_For_Help .. Flame loop
just as easily as you could write "for J in 1 .. 10 loop". It would make
sense to allow a STEP 2 clause for J, because you could add 2 to J. But
you certainly couldn't add 2 to L. No STEP clause would make sense for L.
For consistency, Ada doesn't allow a STEP clause for any FOR loop, even
if the index is numeric.
The reason pointed out earlier, that incorrect loop termination is a
frequent source of error, is certainly another valid reason why Ada
deliberately omitted the STEP clause.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-25 0:00 ` Larry Kilgallen
@ 1997-01-29 0:00 ` Ted Dennison
1997-01-30 0:00 ` Robert Dewar
0 siblings, 1 reply; 8+ messages in thread
From: Ted Dennison @ 1997-01-29 0:00 UTC (permalink / raw)
Larry Kilgallen wrote:
>
> In article <32E93C0B.36A@cloud9.net>, Paul Van Bellinghen <pvanbell@cloud9.net> writes:
> > I'm fairly new to ADA. I have coded in PASCAL, FORTRAN, C, and assembly
...
> > seems to be an obvious omission in the ADA language. The FOR loop does
> > not have a "STEP" option. In order to perform a loop function using a
> > variable's range of values but in steps greater than the variable
> > kernel, you seem to have to use a WHILE loop with the variable
> > incremented within the loop by the step size. Is there a more elegant
...
> As regards FOR loops, you could say:
>
> for BY_ONE from 1 to 20 loop
> declare
> USEFUL_VALUE : INTEGER := BY_ONE*2;
> begin
> my_procedure ( USEFUL_VALUE );
> end;
> end loop;
Wouldn't that allocate and deallocate stack space in a loop? Yuck.
Why not just replace -
my_procedure ( USEFUL_VALUE );
with
my_procedure ( USEFUL_VALUE*2 );
In this case, though, the newbie may be right. The while loop is liable
to be more efficient
--
T.E.D.
| Work - mailto:dennison@escmail.orl.lmco.com |
| Home - mailto:dennison@iag.net |
| URL - http://www.iag.net/~dennison |
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loop step function
1997-01-29 0:00 ` Ted Dennison
@ 1997-01-30 0:00 ` Robert Dewar
0 siblings, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1997-01-30 0:00 UTC (permalink / raw)
Larry Kilgallen wrote:
> As regards FOR loops, you could say:
>
> for BY_ONE from 1 to 20 loop
> declare
> USEFUL_VALUE : INTEGER := BY_ONE*2;
> begin
> my_procedure ( USEFUL_VALUE );
> end;
> end loop;
Ted Dennision responded
>Wouldn't that allocate and deallocate stack space in a loop? Yuck.
>Why not just replace -
> my_procedure ( USEFUL_VALUE );
>with
> my_procedure ( USEFUL_VALUE*2 );
Robert replies
(note: in the following discussion I assume that Larry's Pascal style
slip "from 1 to 20" is replaced by proper Ada: "in 1 .. 20" -- it is
always a good idea to compile examples you post to CLA, since it can
confuse students to see wrong code!)
No, of course not! Any compiler one can imagine using would merge the
frame of the block with the frame of the enclosing procedure. It is only
when objects of large or dynamic length are declared locally in blocks
that there is allocation acivity.
Worrying about the above code is an example of knowing enough to cause
oneself trouble, but not enough to do the right thing -- declaring
scalar objects like this in nested blocks is good Ada style!
The one thing I would change in the above quote is that I would
put the keyword 'constant' before INTEGER (and I would not use all
upper case identifiers :-)
Ted's suggestion of replacing the parameter in the call misses the point.
Remember that we are modeling the effect of step here, and there may
be many references to Useful_Value in the loop.
So, "yuck" is not an appropriate reaction to this code. Larry's general
paradigm for translating stepping loops is good style, and has no
efficiency penalty.
Ted says:
>In this case, though, the newbie may be right. The while loop is liable
>to be more efficient
Robert replies:
This depends on the compiler you are using. Compilers that do extensive
loop optimization may well recognize useful_Value as an induction variable
of the loop.
Then test replacement is possible and By_One can be completely eliminated
from the code yielding code that is equivalent to the for loop. This
is all pretty standard optimization stuff -- see for example the treatment
of optimization in the Dragon book.
Trying this out on GCC, we don't quite do that well (GCC concentrates more
on local peephole optimization rather than on loop optimizations of this
type), but on the other hand, in the program:
with Text_IO; use Text_IO;
procedure t is
Useful_Value : Integer;
begin
for By_One in 1 .. 20 loop
declare
Useful_Value : Integer := By_One*2;
begin
Put (Integer'Image (Useful_Value));
end;
end loop;
Useful_Value := 2;
while Useful_Value < 20 * 2 loop
Put (Integer'Image (Useful_Value));
Useful_Value := Useful_Value + 2;
end loop;
end;
The first loop has only one additional register instruction compared to
the second, hardly enough to get excited about!
It would be interesting to examine what happens on other compilers with
the above test program.
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~1997-01-30 0:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-01-24 0:00 loop step function Paul Van Bellinghen
1997-01-25 0:00 ` Larry Kilgallen
1997-01-29 0:00 ` Ted Dennison
1997-01-30 0:00 ` Robert Dewar
1997-01-25 0:00 ` Matthew Heaney
1997-01-27 0:00 ` johnherro
1997-01-27 0:00 ` Michael Feldman
1997-01-25 0:00 ` Geert Bosch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox