Live data from Hacker News

C's 'while' is spelled 'for' in Go

tour.golang.org

11–20 of 34 posts

Re: C's 'while' is spelled 'for' in Go

#11
post #4

It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Ever since its counterpart "do" was dropped from the for-statement, it's just been utterly nonsensical. I mean how on earth does the word "for" convey a loop? With the recently designed languages like Go and Swift taking so many cues from the Pascal side of the ALGOL language family, how nobody thought to pick up "repe…

> It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Many languages only use `for` to mean a for-each style loop where it reads quite naturally: Python - `for n in range(1,10)` Ruby - `for i in 0..10` Rust - `for n in 1..10` (also picked up the bare `loop`) Others support for-each style loops along with the less than ideal C-style for loop: JavaScript - `for (n of [1,2…

It's only natural if you already know that a for-statement is a loop.

"while" is no better without the "do".

Re: C's 'while' is spelled 'for' in Go

#12
post #9
post #8

Earlier quoted context omitted.

> "Clean this item and put it away. Then do that for every other piece of cooking equipment." Without the "do" it's nonsensical. And that was exactly my point.

That was just one way of phrasing it. You could just as easily put it as, "For every piece of cooking equipment, clean it and put it away."

There you need the word "every" to convey that it's a loop.

I'd certainly agree that "for each" is much better than "for" alone.

Re: C's 'while' is spelled 'for' in Go

#13
post #10
post #8

Earlier quoted context omitted.

> "Clean this item and put it away. Then do that for every other piece of cooking equipment." Without the "do" it's nonsensical. And that was exactly my point.

I think there's an argument that "do" is just a placeholder for the body of the loop, i.e. "clean this item and put it away". If you flip the order that the body and the iteration are expressed so that it reflects what most programming languages use for syntax, it would be "for each piece of cooking equipment, clean it and put it away". There's no "do" here, nor is it needed.

There you need the word "each" to convey that it's a loop.

I'd certainly agree that "for each" is much better than "for" alone.

Re: C's 'while' is spelled 'for' in Go

#14
post #4

It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Ever since its counterpart "do" was dropped from the for-statement, it's just been utterly nonsensical. I mean how on earth does the word "for" convey a loop? With the recently designed languages like Go and Swift taking so many cues from the Pascal side of the ALGOL language family, how nobody thought to pick up "repe…

> It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Many languages only use `for` to mean a for-each style loop where it reads quite naturally: Python - `for n in range(1,10)` Ruby - `for i in 0..10` Rust - `for n in 1..10` (also picked up the bare `loop`) Others support for-each style loops along with the less than ideal C-style for loop: JavaScript - `for (n of [1,2…

Interestingly, the more common way to write that in Ruby would be `(1..10).each { |i| ... }`, or (in the case of needing a multi-line block):

``` (1..10).each do |i| ... end ```

"do" does end up getting used, but only as syntax for a "block" (similar to what other languages would call a lambda).

Ruby also has a shorthand for when you want to do something `n` times without caring about the iteration number:

``` 10.times do ... end ```

One final note is that loop you wrote above is actually different in Ruby than it is for Python and Rust; `x..y` is an inclusive range in Ruby, whereas `range(x, y)` in Python and `x..y` in Rust are exclusive on the right side. I think you might have realized that since you used different starting values for the other two, but you got it backwards; the Python and Rust loops will loop only nine times, whereas the Ruby one will loop 11 times. Ruby does have a syntax for exclusive ranges though; `x...y` is an inclusive range from `x` to `y - 1`. Similarly, in Rust, `x..=y` gives an inclusive range from `x` to `y`. I don't know of any way to do an inclusive range in Python other than manually, but someone with more Python knowledge might be able to provide something for that.

Re: C's 'while' is spelled 'for' in Go

#15
post #14

Earlier quoted context omitted.

> It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Many languages only use `for` to mean a for-each style loop where it reads quite naturally: Python - `for n in range(1,10)` Ruby - `for i in 0..10` Rust - `for n in 1..10` (also picked up the bare `loop`) Others support for-each style loops along with the less than ideal C-style for loop: JavaScript - `for (n of [1,2…

Interestingly, the more common way to write that in Ruby would be `(1..10).each { |i| ... }`, or (in the case of needing a multi-line block): ``` (1..10).each do |i| ... end ``` "do" does end up getting used, but only as syntax for a "block" (similar to what other languages would call a lambda). Ruby also has a shorthand for when you want to do something `n` times without caring about the iteration number: ``` 10.tim…

It's been years since I last wrote Ruby so had completely forgotten that it's ranges were inclusive by default.

Re: C's 'while' is spelled 'for' in Go

#16

Earlier quoted context omitted.

> It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Many languages only use `for` to mean a for-each style loop where it reads quite naturally: Python - `for n in range(1,10)` Ruby - `for i in 0..10` Rust - `for n in 1..10` (also picked up the bare `loop`) Others support for-each style loops along with the less than ideal C-style for loop: JavaScript - `for (n of [1,2…

It's only natural if you already know that a for-statement is a loop. "while" is no better without the "do".

And if statements need a corresponding "then" to make sense as well right?

Your whole argument is nonsensical. Coding isn't English

Write a new language and you can name everything exactly how you please, if enough people care then they will adopt the language.

Re: C's 'while' is spelled 'for' in Go

#17
post #4

It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Ever since its counterpart "do" was dropped from the for-statement, it's just been utterly nonsensical. I mean how on earth does the word "for" convey a loop? With the recently designed languages like Go and Swift taking so many cues from the Pascal side of the ALGOL language family, how nobody thought to pick up "repe…

All languages, both human and programming, have vestigial nonsense like this. All of them. It isn't possible for languages which are used to not have them, I would say.

Re: C's 'while' is spelled 'for' in Go

#18

Looking at the syntax (as a Go beginner), I'm not really sure whether that's less or more to learn. Seems like one of those arbitrary language design decisions that don't make a huge difference either way.

Yeah when I picked up Go, the infinite loop caught me by surprise:

  for {
    ...
  }

Re: C's 'while' is spelled 'for' in Go

#19

Looking at the syntax (as a Go beginner), I'm not really sure whether that's less or more to learn. Seems like one of those arbitrary language design decisions that don't make a huge difference either way.

Yeah when I picked up Go, the infinite loop caught me by surprise: for { ... }

Looks like a minor simplification of

  for (;;) {
     ...
  }
I'm biased by my experience with c, but I do like the explicit punctuation.

Re: C's 'while' is spelled 'for' in Go

#20
post #4

It's kind of sad that "for" is the keyword programming languages settled on for the loop construct. Ever since its counterpart "do" was dropped from the for-statement, it's just been utterly nonsensical. I mean how on earth does the word "for" convey a loop? With the recently designed languages like Go and Swift taking so many cues from the Pascal side of the ALGOL language family, how nobody thought to pick up "repe…

Python's "for customer in customers" can't be clearer in my opinion.
Post reply on HN