Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Agreed. I find the half-open ranges impossible and stumble over them every time.

They are inconsistent within two characters: closed on one side, then open on the other.

If the notation was >my_list[0:3) at least it would be mathematically appropriate.



See the Dijkstra essay[1] for a convincing argument for half-open ranges.

https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831...


I have read it, and Dijkstra is a good call to authority, but I don't agree.

His opening sentence is instructive: using the 'pernicious three dots', everything is totally clear: with an ellipsis, it's impossible to understand anything other than a closed interval.

It's primary school mathematics that 2 <= x <= 12, i.e. the interval mathematically written [2,12] (or 2,...,12), contains 11 integers. A programmer who doesn't understand that is in a world of trouble as half-open intervals won't stop the inevitable rain of off-by-one errors.

A programmer who actually calculates the size of a list in the way that Dijkstra suggests (specifically, by calculating b-a) is also perhaps acting questionably. Dijkstra's argument here might make more sense when correctly framed in 1982.

As an aside, if Dijkstra would agree to use a fully-closed interval, his suggested advantage of 0-indexing is eliminated and he would presumably then prefer 1-indexing.


That's not the entirety of the argument. There's the natural number stuff, as well.

Also, b-a is appealing because simplicity is a good indicator you're on the right track, and part of the definition of elegance.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: