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


Groups > comp.programming > #1957

Re: repeating something n times

From "BartC" <bc@freeuk.com>
Newsgroups comp.programming
References <repeat-20120705210751@ram.dialup.fu-berlin.de> <jt7jdv$247$1@dont-email.me> <b73472b3-9e35-45e4-b9cf-ab8366f4e770@googlegroups.com> <jtsqtf$q73$2@dont-email.me> <R-ydnZ86Os78q5_NnZ2dnUVZ_hSdnZ2d@giganews.com>
Subject Re: repeating something n times
Message-ID <pgxMr.866349$ME5.164830@fx22.am4> (permalink)
Organization virginmedia.com
Date 2012-07-15 11:49 +0100

Show all headers | View raw


"Aaron W. Hsu" <arcfide@sacrideo.us> wrote in message
news:R-ydnZ86Os78q5_NnZ2dnUVZ_hSdnZ2d@giganews.com...
> "BartC" <bc@freeuk.com> writes:
>
>>Isn't that a lot simpler than the the thirteen tokens of
>>"for(i=0;i<3;i++)"
>>which also have half-a-dozen ways of getting it wrong in a way the
>>compiler
>>won't pick up?
>
> If you know C, this isn't that hard, and while it is arguably simpler to
> read the other one if you do not know the language, I would say that you
> had
> best not try to interpret what a piece of code is going without thoroughly
> understanding the language. The benefit of C's for loop compared to a
> host of special purpose syntactic constructs is that you have a common
> vocabulary for doing a whole lot of operations. In some sense that makes
> your life easier, and in another sense it makes it harder.

You could also argue that using 'if' and 'goto' statements are common
vocabulary for creating any conceivable language construct! Using dedicated
statements for certain types of loops has the same advantage as using 'for',
'while' and 'do' instead of 'if' and 'goto'.

> As noted elsewhere, it's not clear whether repeat (4) is meant to be
> doing four times or whether it is repeating up to, but not including 4.

Your thinking is zero-based. That languages I create are one-based. (That 
also means certain types of loops are awkward in languages such as C which 
like to count 0 to N-1.)

> To a C programmer, that's immediately clear in the use of < rather than
> <=.
> It's nice to have conveniences, but they must be learned like anything
> else.

Worrying about using < or <= is the compiler's job. Yes even in a low-level
language like C.

>>Malcolm:
>>> buff[low:high] = buff2[low2+1:high2];
>
>>I doubt you would be any wiser seeing it written as:
>
>> for (i=low; j=low2+1; i<=high; ++i; ++j)
>>   buff[i] = buff[j];
>
> For someone familiar with the first syntax, that will be easier, and for
> someone familiar with the second, that will be easier. However, the idiom
> you often see for this in C is not the above, but rather the following:
>
> i = low; dst = buff + low; src = buff2 + low2 + 1;
> while (i++ < high) *dst++ = *src++;
>
> It's not better than the others, it is just different.

There are any number of ways to write it using basic statements. But the
more statements that are needed, the more chance for error, obfuscation, and
need to take your mind off the bigger picture of what you're trying to do.
The same goes for any reader of the code, who will just see extra clutter!

> A C programmer
> would be asking themselves whether high is an inclusive or exclusive
> upper bound, and so on.

C is zero-based, so ranges would likely have an exclusive top end, like
Python. But that need only be a problem for someone who insists on
understanding the code at an element-by-element level. However that need not
be necessary; for example:

 a := b                 # copy array b to a
 a[3:] := b[:3]

The latter might copy the last 3 elements of b, to the first 3 elements of
a. But at no point do you need to know how arrays are indexed.

You're saying these details always need to be exposed? What happened to
abstraction?

(I won't go into whether that a:=b assignment does a deep or shallow copy; 
that's language dependent among other things. You don't really want to 
'help' the reader by actually writing the  dozens of lines of code to do 
nested, deep list copies in-place!)

> C makes a lot of things explicit, for better or worse. APL makes a lot
> of things implicit, for better or worse. Each has its place.

You've chosen two languages at opposite ends of the spectrum! I'm just
saying it's possible to make life a bit easier with languages at the C end
of the scale; they don't need to be revolutionised.

-- 
Bartc 

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-06 21:56 +0100
  Re: repeating something n times Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-06 22:16 +0100
    Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-06 23:18 +0100
  Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 14:11 -0700
    Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-14 23:11 +0100
      Re: repeating something n times Ike Naar <ike@iceland.freeshell.org> - 2012-07-14 23:37 +0000
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 01:16 +0100
          Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 17:35 -0700
            Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:23 -0500
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:19 -0500
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 11:00 +0100
      Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-14 17:44 -0700
      Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 22:17 -0500
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 11:49 +0100
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-15 09:53 -0500
      Re: repeating something n times Rui Maciel <rui.maciel@gmail.com> - 2012-07-15 12:18 +0100
        Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 13:21 +0100
          Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 13:02 +0000
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 14:36 +0100
              Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 14:18 +0000
                Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 15:45 +0100
                Re: repeating something n times Willem <willem@toad.stack.nl> - 2012-07-15 16:25 +0000
                Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 18:02 +0100
          Re: repeating something n times Rui Maciel <rui.maciel@gmail.com> - 2012-07-15 15:10 +0100
            Re: repeating something n times "BartC" <bc@freeuk.com> - 2012-07-15 16:10 +0100
              Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-15 13:00 -0700
    Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-14 21:55 -0500
      Re: repeating something n times "io_x" <a@b.c.invalid> - 2012-07-17 20:47 +0200
        Re: repeating something n times "io_x" <a@b.c.invalid> - 2012-07-18 14:19 +0200
          Re: repeating something n times Aaron W. Hsu <arcfide@sacrideo.us> - 2012-07-18 12:55 -0500
            Re: repeating something n times malcolm.mclean5@btinternet.com - 2012-07-18 11:57 -0700

csiph-web