Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.misc > #2143

Re: 100 Things

From "James Harris" <james.harris.1@gmail.com>
Newsgroups comp.lang.misc
Subject Re: 100 Things
Date 2014-05-02 16:42 +0100
Organization A noiseless patient Spider
Message-ID <lk0ecl$ihg$1@dont-email.me> (permalink)
References (6 earlier) <85e8v.227832$gV7.159905@fx21.am4> <ljs1cm$kig$1@dont-email.me> <Nsq8v.316223$%n2.142416@fx30.am4> <ljvr0j$cgl$1@dont-email.me> <EfM8v.147725$vM2.40475@fx18.am4>

Show all headers | View raw


"BartC" <bc@freeuk.com> wrote in message 
news:EfM8v.147725$vM2.40475@fx18.am4...
> "James Harris" <james.harris.1@gmail.com> wrote in message
> news:ljvr0j$cgl$1@dont-email.me...
>> "BartC" <bc@freeuk.com> wrote in message
>
>>>I use:
>>>
>>> for i:=A to B do            Step +1
>>> for i:=A downto B do        Step -1
>>> for i:=A to B by C do
>>> for i:=A downto B by C do
>>>
>>> With the 'by' forms expecting C to be a constant (either positive or
>>> negative; it will make 'to' into 'downto' as needed). (If not, it is
>>> assumed
>>> to be positive or negative respectively).

I've just read that again and it still seems complicated. Have I got the 
following right?

  for i := A to B by C do (i.e. using *to*)
  - if C is a constant and positive the loop will count up
  - if C is a constant and negative the loop will count down
  - if C is a variable the loop will count up

  for i := A downto B by C do (i.e. using *downto*)
  - if C is a constant and positive the loop will count down (?)
  - if C is a constant and negative the loop will count down
  - if C is a variable the loop will count down

>>> (BTW the 'downto' method is from Pascal, which I thought was a good
>>> simplification in forcing a loop direction to be known at compile-time.)
>>
>> If "to" always meant "upto" and there was a separate "downto" the
>> direction issue is resolved. Of course, the word "to" doesn't sound like
>> it is always ascending and IIRC a Basic allowed "to" to step downwards as
>> in
>>
>>  for P = 10 to 1 step -1
>>
>> But if "to" can mean either direction depending on the use of "by" that
>> sounds a little complicated!
>
> Lua just uses "," in place of "to". In that sense, "," defines both
> directions, but needs an explicit -1 step for a downward loop. So the idea
> was that "to" could be used in the same way, if the user wants to:
>
> for i:=a to b by 1
> for i:=a to b by -1
>
> The "downto" form is an easy and obvious alternative to the -1 step, 
> however
> an explicit step size here is more ambiguous:
>
> for i:=a downto b by 1
> for i:=a downto b by -1
>
> Do these both count downwards? Perhaps the compiler apply -abs() to the 
> step
> which is added to at each iteration.

I'm not sure if you are saying your favoured loop works that way or not but 
none of those issues need to arise. With C the questions don't need to be 
asked. Nor do the answers need to be remembered.

>>> This is easy. You just don't worry about it. Or prohibit it when the
>>> compiler can't figure out what to do.
>>
>> Umm, not sure about that. Consider
>>
>>  S = function_call();
>>  for P = Q to R step S;
>>
>> Can the compiler figure out what to do? Probably not. Now compare that
>> with
>>
>>  S = -1;
>>  for P = Q to R step S;
>>
>> Can the compiler figure out here what to do?
>
> But what about:
>
> for P = Q to R step function_call();
>
> Here the loop could be oscillating backwards and forwards! The sensible
> thing here is calculate the step only at the start then have it invariant
> (if it isn't already so).

That depends on whether the step is resolved once before the loop starts or 
at the beginning of each iteration. And it is not apparent from the syntax 
(as discussed in previous posts).

> A loop which is always counts up or down, but which it is is not known 
> until
> runtime, a compiler can just about deal with (but will be inefficient as
> this check needs to be done per iteration, unless you duplicate the loop 
> body). One that constantly change direction would be too messy though, and 
> too odd for a compiler to spend too much time supporting.
>
> My decision (and Pascal's) is that a loop direction must be determinable 
> at
> compile time.

Going a bit off topic here but if you want to use that loop form why not 
just have upto and downto keywords? Then the loop direction is always 
immediately clear.

...

>> If you wanted to see a 90+% run of standard up or down parameters I bet
>> you are disappointed! ;-) In reality this is just a sample piece of code
>> and is not meant to be typical.
>
> But in this sample:

I did say this was not representative so you should not read too much in to 
it. But I can see from your reply that you think of these loops as different 
sorts. That's your preference.

My own language goes even further than C and reduces down to one loop type. 
After all a loop is a loop. You would love it. ;-)

...

>> We started this subthread because you expressed surprise that few
>> languages had added anything simpler to C's for loop. I have tried to
>> explain why I think that C's approach is so good that it doesn't need the
>> addition you would like, and why that addition can bring with it a lot of
>> unwanted baggage.
>
> Well, OK. I think (for most of the above examples) having to introduce a 
> variable i, initialise it, increment it, and compare it to N, is also 
> quite a bit of baggage compared with just writing N!

By baggage I meant the issues and questions that are associated with those 
kinds of loops but are not apparent in the syntax, i.e. all the questions we 
have discussed and for which there is no universally accepted model so that 
different languages use different choices.

Having said that, I should add that I could work with a language which had a 
simplified X to Y loop - as long as I could remember the answers to all the 
questions, that is. ;-)

James

Back to comp.lang.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

C: 100 Things "BartC" <bc@freeuk.com> - 2014-04-29 17:57 +0100
  Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 07:40 +0100
    Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 12:16 +0100
      Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 13:54 +0100
        Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 14:39 +0100
          Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-04-30 20:18 +0100
            Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 22:40 +0100
              Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-04-30 22:09 +0000
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-04-30 23:36 +0100
                Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 11:03 +1200
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:47 +0100
              Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-01 00:35 +0100
                Re: 100 Things Andy Walker <news@cuboid.co.uk> - 2014-05-01 11:03 +0100
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 20:16 +0100
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 12:44 +0100
                Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-02 11:11 +0100
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 13:32 +0100
                Re: 100 Things "James Harris" <james.harris.1@gmail.com> - 2014-05-02 16:42 +0100
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 17:54 +0100
          Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 11:01 +1200
      Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-04-30 21:13 -0400
        Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:26 +0100
          Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
      Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 13:42 +1200
        Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 09:35 +0100
          Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 21:02 +1200
            Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 10:40 +0100
              Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-01 22:06 +1200
          Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 11:08 +0200
            Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 11:02 +0100
              Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 13:57 +0200
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 15:12 +0100
                Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 17:18 +0200
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 17:04 +0100
                Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-01 18:30 +0000
                Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
                Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-02 08:30 +1200
                Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-03 01:29 -0400
                Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-03 02:37 -0400
                Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-03 15:39 -0400
                Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-03 17:30 -0400
                Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-04 20:05 +1200
                Re: 100 Things George Neuner <gneuner2@comcast.net> - 2014-05-04 13:28 -0400
                Re: 100 Things Ian Collins <ian-news@hotmail.com> - 2014-05-05 07:40 +1200
                Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-01 22:00 +0200
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-01 23:24 +0100
                Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-02 08:59 +0200
                Re: 100 Things "BartC" <bc@freeuk.com> - 2014-05-02 09:53 +0100
                Re: 100 Things "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> - 2014-05-02 11:22 +0200
                Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-01 18:26 +0000
                Re: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-05-01 16:08 -0400
                Re: 100 Things Kaz Kylheku <kaz@kylheku.com> - 2014-05-02 02:49 +0000
  Re: C: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-04-30 22:08 -0400
  Re: C: 100 Things "BartC" <bc@freeuk.com> - 2014-07-25 23:53 +0100
    Re: C: 100 Things "Rod Pemberton" <dont_use_email@xnothavet.cqm> - 2014-07-26 05:38 -0400
      Re: C: 100 Things "BartC" <bc@freeuk.com> - 2014-07-26 13:51 +0100
        Re: C: 100 Things rick.c.hodgin@gmail.com - 2014-10-25 04:48 -0700
  Re: C: 100 Things "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2014-10-25 04:50 -0700

csiph-web