5 ms·
Depends on whether you're measuring or counting. The article mentions Mesa's interval syntax, which gave programmers all the possible options: [1..100] (0 ..
by Animats 7y ago
Depends on whether you're measuring or counting.
The article mentions Mesa's interval syntax, which gave programmers all the possible options:
[1..100] (0 .. 101) [1 .. 101) [1 .. 101)
were all the same interval, 1 to 100 inclusive. Bad idea. Great way to get off-by-one errors.
Another bad idea from that era - Wirth's insistence that you shouldn't be allowed compile-time arithmetic in Pascal. Like.
const
tabsize = 100;
var
tab: array [0..tabsize-1] of integer; { NOT ALLOWED }
No, you had to have a separate named constant for "tabsize-1", and it couldn't be initialized as "tabsize-1", either.
- lordalch 7y agoIsn't that just how you get `const ONE = 1` cruft?
- Someone 7y ago”Bad idea. Great way to get off-by-one errors.” I would think that depends on whether that gets taught at schools early in life. Intervals that are closed at the low and open at the high end also are convenient when using zero-based indexing (you want an easy way to express 0,1,2,…,n-1, and if you’re used to the notation [0,n) is nicer than [0,n-1]) I would think overloading the meaning of the various parentheses makes parsing and generating clear error messages harder, though (but not as hard as when one would follow ISO 31-11 and allow such things a ]3,7] for an half-open interval excluding 3, but including 7) I think the desire to not overload parentheses with yet another meaning is why ‘Modern’ languages tend to use an infix operator for ranges, e.g. ‘1 to 5’ or ‘1...5’, with Swift having the half-open variant ‘1..<5’ (IMO ugly, but clear, so I guess one would get used to it).
- a1369209993 7y ago> you want an easy way to express 0,1,2,...,n-1 I'm partial to the viciously set-theoretic[0]: for i in 5: print i # 0;1;2;3;4 0: finite ordinals[1] are 0={},1={{}}={0},2={{},{{}}}={0,1},3={{},{{}},{{},{{}}}}={0,1,2},Sx=x∪{x}={0,...,x} 1: aka naturals or nonnegative/unsigned integers
- Zarel 7y agoThat only works if you're ranging from 0 to n-1, though. A popular way to partition an array at i and j is to do: arr[0..<i], arr[i..<j], arr[j...-1]
- a1369209993 7y agoa0,a1,a2 = arr.splitat({i,j}) maybe? It avoids repeating i and j twice and arr thrice, at least. Admittedly, there probably are cases where you really do want a range [x..^y] for arbitrary x and y, but it seems like most cases aren't.
- FisDugthop 7y agoSwift may have borrowed it from E, where the half-open variant is `1..!5`, read "from 1 up to, but not including, 5". It is extremely useful for 1-to-n sorts of counting tasks.
- WalterBright 7y ago> 'Modern' languages After having many fencepost errors, I finally came to the conclusion that all my code shall henceforth be zero-based. I'm much happier not having those errors anymore. I.e.: for (i = 1; i <= N; ++i) The <= is always a huge red flag for me, and I rewrite it as: for (i = 0; i < N; ++i) and add a `1` in the body if I must.
- chopin 7y agoAny specific reason to use ++i over i++? I always use the latter but only out of habit. I've rarely seen code with the former (your idiom).
- lmm 7y agoIn theory extremely naive compilers may copy `i` if you use `i++` (since `i++` evaluates to the old value, whereas ++i can always be a destructive update), so some programmers have a habit of defaulting to ++i.
- WalterBright 7y agoIt's just better form. Also, if `i` is a struct with an overloaded postinc operator, it can be more expensive.
- jstimpfle 7y agoI think that is arguable. The form "array[idx++] = foo;" has undeniable elegance, and I still use it sometimes, even though I've developped a pretty verbose coding style in general.
- chopin 7y agoYeah, but why is it better form (I am coming from Java where it shouldn't make any difference)?
- WalterBright 7y agoIt's better form to not ask for a characteristic that you don't need. For example: 1. increment x 2. take the value of x, then increment x (1) is better form if you don't need the value of x.
- sedatk 7y agoI think modern Pascal implementations fixed some of those.
- maxdamantus 7y ago> Depends on whether you're measuring or counting. You run into issues when using 1-based counting as well as measuring. Our calendar years use 1-based counting, which results in things like "2020" being the "20th year" of the "21st century". If we used 0-based counting instead, we would call the current year "2019" and it would be "year 19" of "century 20". Explanation of conventional year counting: 1st century began in year 1 2nd century began in year 101 (100 years later) .. 21st century began in year 2001 If you run the same sequence as above, but start counting from 0, you get seemingly much more sensible numbers (0, 0; 1, 100; 20, 2000; x, x*100...). I suspect most people probably think that the 21st century began in 2000 anyway. It should also be noted that the use of the number 0 in general is relatively recent. The Roman numeral system (which was initially used for our year numbering) has no representation for it, as it was only used conceptually hundreds of years later. It seems to me that the main reason people count from 1 is historical.
- supportlocal4h 7y agoCounting from 0, 2019 would still be the 19th year of the 21st century. I'm puzzled why you completely changed grammar to make it seem like counting from zero also solved the century mismatch. If anything it suggests that counting from 1 is more consistent. Then 2020 is year 20 of century 20. But then 2000 becomes year 0 of century 20, which is confusing English. But "century 20" isn't great English anyway. Calendars should be 0-based like any other measuring tool. The 3rd minute of the 2nd hour of a marathon is 1:02. The 3rd day of the 2nd month of my marathon is 1/02. If that happens to be the 3rd day of the second month of the year, it's 2/03?
- kijin 7y agoThere's another gotcha. There is no year 0 CE. It goes straight from -1 to 1. That's going to be a problem for zero-based counting!
- OscarCunningham 7y agoIt doesn't go from -1 to 1, it goes from 1 BCE to 1 CE. Negatives aren't used at all. Personally I think we should use zero and negative CE to extend backwards instead. So the year we currently refer to as 1 BCE becomes 0 CE, 2 BCE becomes -1 CE and N BCE becomes -N+1 CE.
- beagle3 7y agoNim (which is inspired in part by Pascal and Ada) uses “a..b” to denote a closed interval and a..<b to denote an interval half-open on the right. The language is flexible enough to also let you define a<..b and a<.<b to complete the quartet, but I don’t know if anyone who ever did.
- mikorym 7y agoThe set of natural numbers should have zero, otherwise it's not a ring. However, from the perspective of Peano's axioms, it doesn't matter as far as I am concerned what the symbols are called, as long as you have a successor function. Apart from that, I agree that it depends whether you are counting [1] or doing something else. [1] That's why I do HN references like this.
- hinkley 7y agoOof. I bet it was a barrel of laughs working out how many minutes are in the month of July around him.
- augustk 7y ago> Another bad idea from that era - Wirth's insistence that you shouldn't be allowed compile-time arithmetic in Pascal. This restriction has since long been relaxed in Wirth's later languages Modula-2 and Oberon which both supersede Pascal. https://www.miasap.se/obnc/oberon-report.html#appendix https://www.miasap.se/obnc/oberon-report.html#appendix
- pjmlp 7y agoExcept that even Turbo Pascal for MS-DOS allowed it. People should actually stop using late 70's Pascal examples, specially when ISO Extended Pascal in 1990 fixed most of them, not to mention famous dialects like UCSD, Apple's Object Pascal and Turbo Pascal, or its modern variants.