by Someone · 6y ago · 0 comments
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).
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.Given the author, I assume that this is also common in D.
The argument I've heard for why in C++ prefix is preferred is that compilers have a harder time optimizing out the temporary object cruft (especially older compilers).
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
A popular way to partition an array at i and j is to do:
arr[0..<i], arr[i..<j], arr[j...-1] a0,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.