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

Smalltalk actually has two different ways to address things: as sequences, and as streams. Sequences are 1-based, and streams are 0-based. This is designed to model what you intuitively do in real life. As a picture:

    |  1  |  2  |  3  |  4  |
    +-----+-----+-----+-----+
    0     1     2     3     4
Think of a ruler. The ruler starts at zero, but you don't refer to the first inch as inch zero; it's the first inch, or inch 1. But if you want to delineate the inch, you do indeed say from the beginning of the ruler (i.e., 0) to the first inch marker (i.e., 1).

I'm not going to tackle whether Djikstra was right or wrong, but merely to point out that there's more to Smalltalk's indexing than meets the eye.

For what it's worth: I'm not a professional Smalltalk dev by any stretch of the imagination, but I've written enough Smalltalk to tell you that you very rarely deal with the indices, anyway. Nearly all Smalltalk code ends up just using comprehensions to navigate collections. Of the remainder, they're such trivial cases that the 1-based indexing is intuitive and does not bother me the way that it does in, say, Lua or really old Turbo Pascal programs where some coder got a bit too slap-happy on arbitrarily bounded arrays.



Math vs. Language.

I don't refer to the first inch of a ruler as inch 0 or inch 1, I refer to it as the first inch.

There are two things being described which I think is a set up for confusion and the many off by one errors that I've seen as a result.

    1. Coordinate.
    2. Interval.
The origin of a graph using the Cartesian coordinate system is usually 0,0, not 1,1.

But, in my list of 2 items, I index with 1 and 2, not 0 and 1. But, be sure that the first item is 1.0 not 1.1 and the second item is 2.0 not 2.1.




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

Search: