From: Robert Redelmeier on
In alt.lang.asm [FUp set] Sjouke Burry <burrynulnulfour(a)ppllaanneett.nnll> wrote in part:
> Nick Keighley wrote:
>> lost me. So far as I remember FORTRAN didn't have recursion. Am I
>> wrong? Or are you saying even modern Fortran doesn't have recursion?
>
> If recursion does not exist, I will have to ditch quite
> a couple of fortran programs. There is only 1 hitch, you
> have to write a 3 line dummy routine, to do the calling,
> so a calls b , b calls a. fortran MS 5.1 and later.
> Also Fortran77 for Riscos (arc 320 computer)


For what problems do you find recursion to be the fastest
[CPU runtime] solution method?

-- Robert

From: Seebs on
On 2010-05-09, Daniel T. <daniel_t(a)earthlink.net> wrote:
> While both of the above are much worse than:

> int i = 0;
> while (i < N && !foo(i))
> ++i;
> return i;

I'm not convinced of it.

> The above is a standard idiom in C++.

How nice for it?

In C, the idiom of using a for loop to iterate over arrays is pretty
firmly entrenched.

I just noticed the crossposting, so I'm dropping the extra groups.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: Nick on
"Daniel T." <daniel_t(a)earthlink.net> writes:

> Seebs <usenet-nospam(a)seebs.net> wrote:
>> On 2010-05-08, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> > Seebs wrote:
>> >> On 2010-05-02, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> >> Because, in some cases, the checks against a[i] make it harder to follow
>> >> the code.
>>
>> > In my experience, it is generally easier to follow the code if it has a
>> > clear structure.
>>
>> I would agree that it is *GENERALLY* easier.
>>
>> But in some cases it's not.
>>
>> The vanilla loop idiom ("for (i = 0; i < N; ++i)") has a HUGE weight of
>> idiomatic clarity -- it does not require noticable attention to model it,
>> and that clarity is lost as soon as you change the loop condition.
>>
>> In short:
>> for (i = 0; i < N; ++i)
>> if (foo(i))
>> return i;
>> return -1;
>>
>> is nearly always much easier for a programmer to grok in fullness than
>> for (i = 0; i < N && !foo(i); ++i)
>> ;
>> if (i != N)
>> return i;
>> else
>> return -1;
>
> While both of the above are much worse than:
>
> int i = 0;
> while (i < N && !foo(i))
> ++i;
> return i;
>
> The above is a standard idiom in C++.

That one doesn't distinguish between "found" and "not found". I don't
think there's much to chose between

> int i = 0;
> while (i < N && !foo(i))
> ++i;

and

>> for (i = 0; i < N && !foo(i); ++i)
>> ;

when you present them like that you can see they are so close as makes
no difference. It's that bit of Seeb's code that you've not copied that
makes the the single exit point version messy.

And, of course, Seeb's isn't as good as it should be - he still has two
return points. So each of these, to be loop-politically-correct should
really end:

return (i != N) ? i : -1;
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
From: Daniel T. on
Nick <3-nospam(a)temporary-address.org.uk> wrote:
> "Daniel T." <daniel_t(a)earthlink.net> writes:
> > Seebs <usenet-nospam(a)seebs.net> wrote:
> > > On 2010-05-08, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
> > > > Seebs wrote:
> > > > > On 2010-05-02, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
> > > > >
> > > > > Because, in some cases, the checks against a[i] make it harder
> > > > > to follow the code.
> > >
> > > > In my experience, it is generally easier to follow the code if
> > > > it has a clear structure.
> > >
> > > I would agree that it is *GENERALLY* easier.
> > >
> > > But in some cases it's not.
> > >
> > > The vanilla loop idiom ("for (i = 0; i < N; ++i)") has a HUGE
> > > weight of idiomatic clarity -- it does not require noticable
> > > attention to model it, and that clarity is lost as soon as you
> > > change the loop condition.
> > >
> > > In short:
> > > for (i = 0; i < N; ++i)
> > > if (foo(i))
> > > return i;
> > > return -1;
> > >
> > > is nearly always much easier for a programmer to grok in fullness
> > > than
> > > for (i = 0; i < N && !foo(i); ++i)
> > > ;
> > > if (i != N)
> > > return i;
> > > else
> > > return -1;
> >
> > While both of the above are much worse than:
> >
> > int i = 0;
> > while (i < N && !foo(i))
> > ++i;
> > return i;
> >
> > The above is a standard idiom in C++.
>
> That one doesn't distinguish between "found" and "not found".

Of course it does, and it does so more efficiently. For the other
examples "if (i != N)" is checked and then later "if (result != -1)"
must be checked. With the example I presented only one "if" is necessary.

> ... It's that bit of Seeb's code that you've not copied that makes the
> the single exit point version messy.

It's the bit that I didn't copy that makes the code cleaner and reduces
overall code size and complexity.

I'm more than happy to accept that using a 'while' that actually has a
loop body, vs using a 'for' with an empty loop body is a style issue,
but considering the back and forth between how to effectively express an
empty loop body so that it won't be interpreted as a mistake, and the
fact that the initialized variable ("i" in this case) actually needs to
be used after the loop exits, I think my style is a better choice in
this regard.

(Note here, I'm not arguing for single exit in every case, but it's
clearly a better choice in *this* example.
From: Nick on
"Daniel T." <daniel_t(a)earthlink.net> writes:

> Nick <3-nospam(a)temporary-address.org.uk> wrote:
>> "Daniel T." <daniel_t(a)earthlink.net> writes:
>> > Seebs <usenet-nospam(a)seebs.net> wrote:
>> > > On 2010-05-08, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> > > > Seebs wrote:
>> > > > > On 2010-05-02, Richard Heathfield <rjh(a)see.sig.invalid> wrote:
>> > > > >
>> > > > > Because, in some cases, the checks against a[i] make it harder
>> > > > > to follow the code.
>> > >
>> > > > In my experience, it is generally easier to follow the code if
>> > > > it has a clear structure.
>> > >
>> > > I would agree that it is *GENERALLY* easier.
>> > >
>> > > But in some cases it's not.
>> > >
>> > > The vanilla loop idiom ("for (i = 0; i < N; ++i)") has a HUGE
>> > > weight of idiomatic clarity -- it does not require noticable
>> > > attention to model it, and that clarity is lost as soon as you
>> > > change the loop condition.
>> > >
>> > > In short:
>> > > for (i = 0; i < N; ++i)
>> > > if (foo(i))
>> > > return i;
>> > > return -1;
>> > >
>> > > is nearly always much easier for a programmer to grok in fullness
>> > > than
>> > > for (i = 0; i < N && !foo(i); ++i)
>> > > ;
>> > > if (i != N)
>> > > return i;
>> > > else
>> > > return -1;
>> >
>> > While both of the above are much worse than:
>> >
>> > int i = 0;
>> > while (i < N && !foo(i))
>> > ++i;
>> > return i;
>> >
>> > The above is a standard idiom in C++.
>>
>> That one doesn't distinguish between "found" and "not found".
>
> Of course it does, and it does so more efficiently. For the other
> examples "if (i != N)" is checked and then later "if (result != -1)"
> must be checked. With the example I presented only one "if" is
> necessary.

Am I being particularly dumb here (it's been known). Are you saying
that you elsewhere in the code you use i!=N as the check? In which
case, I suppose so, but it's a pain to use if 'N' isn't easily
calculable or available later (say it's not actually i<N but a test
against a sentinal).
>
>> ... It's that bit of Seeb's code that you've not copied that makes the
>> the single exit point version messy.
>
> It's the bit that I didn't copy that makes the code cleaner and reduces
> overall code size and complexity.
>
> I'm more than happy to accept that using a 'while' that actually has a
> loop body, vs using a 'for' with an empty loop body is a style issue,
> but considering the back and forth between how to effectively express an
> empty loop body so that it won't be interpreted as a mistake, and the
> fact that the initialized variable ("i" in this case) actually needs to
> be used after the loop exits, I think my style is a better choice in
> this regard.

I'm coming to that view myself. It also prevents some clever person
coming along and sticking an "int" inside the first clause of the for
loop.

> (Note here, I'm not arguing for single exit in every case, but it's
>clearly a better choice in *this* example.

Only if you're prepared to check against N whenever you need to (or - as
I've said - I'm missing the blindingly obvious).
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk