The Pebble in the Ruby Shoe

On Wed, 2007-07-18 at 22:56 +0900, Robert D. wrote:

Lets not forget also that doing things for the sake of being different help no
one. I for one must work in multiple languages. I still haven’t forgiven Matz
for the whole, “0 is true” thing. :wink:
That is the single best feature of Ruby once you got used to it –
with “” is true!!!

One of the biggest advantages to this is that it makes || and ||=
actually useful for non-boolean stuff (by way of contrast, Perl has the
same operator behavior, but it’s not as useful when common valid values
can be false).

-mental

On 7/18/07, MenTaLguY [email protected] wrote:

On Wed, 2007-07-18 at 22:56 +0900, Robert D. wrote:

Lets not forget also that doing things for the sake of being different help no
one. I for one must work in multiple languages. I still haven’t forgiven Matz
for the whole, “0 is true” thing. :wink:
That is the single best feature of Ruby once you got used to it –
with “” is true!!!

One of the biggest advantages to this is that it makes || and ||=
and one of its biggest dangers too, I wonder how much time has been
spent in looking for “Why has my already (with false) initialized
variable been overwritten?”
I am not asking that false evaluates to true, but I suggest that

x=false
x||=42
x → false

or even
x ??= 42
for that semantics.

Robert

On Wed, Jul 18, 2007 at 08:54:36PM +0900, Robert D. wrote:

1-based indices make simpler is getting the last element of an array
given its size; everything else seems to get more complex.

I do not like categorical statements, but this one might be the
exception that confirms the rule.
+1 (or was that +0 :wink:

Actually, that should probably be ++.

That brings up a good point, though. For rating systems and other needs
that work on similar principles, you’re better off with zero-based
arrays.

On Thu, Jul 19, 2007 at 02:10:08AM +0900, John W. Kennedy wrote:

With 50+ years of collective experience, it seems clear that zero-
based arrays lead to the simplest code.

…unless there is a lot of exposure of nominal index values to the end
user, in which case the additional effort of doing that may incur more
complexity than is saved by using 0-origin internally.

Not always true. Think of voting systems, such as on reddit, where the
positive value of something is the sum of all the increment-votes and
decrement-votes assigned to it. The index value in a zero-based array
would be exactly what you’d want to expose to the end-user in such a
circumstance. Of course, such systems aren’t always (or even usually,
perhaps) managed with an array – but when you’re trying to keep track
of
who up- or down-voted something, as well as how many votes it got, an
array starts to look more interesting.

Hi –

On Thu, 19 Jul 2007, Robert D. wrote:

One of the biggest advantages to this is that it makes || and ||=
and one of its biggest dangers too, I wonder how much time has been
spent in looking for “Why has my already (with false) initialized
variable been overwritten?”
I am not asking that false evaluates to true, but I suggest that

x=false
x||=42
x --> false

But x ||= 1 is really: x = x || 1. So you’d end up with,
essentially, false || 1 being false, which wouldn’t make sense, and
sort of would be false evaluating to true :slight_smile:

David

On Jul 16, 8:22 am, Raphael G. [email protected]
wrote:

The pebble in the Ruby shoe is the counter-intuitive use of indexing of
elements of arrays, strings, etc., from 0 to n-1 instead of the more
natural 1 to n.

You’re saying that it is self-evident that indexing
should start at 1. Remember the definition of “self-evident”:

self-evident, adj. Evident to one’s self and to nobody else.

Let’s say you’re programming in a low-level language.
If A is the address of an array, and S is the size
in bytes of an element, what’s the address of the
Nth element?

If indexing is 0-based:
A + N*S

If indexing is 1-based:
A + (N-1)*S

Can you tell which is simpler?

In article
[email protected],
Tim P. [email protected] wrote:

(ducks)…

Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.

Well, if one were to blindly follow one’s predecessors, yes - at least
with Fortran. Pascal lets you determine the array range, so you could
have the option of starting with zero. And indeed, when we first
encountered that possibility, zero got used a lot.

See, you are assuming that C worked some sort of mind control on poor
weak-willed programmers. Problem is, it isn’t true. Being forced to
use 1 as the base index was actually a pain - the off-by-one problem
occurred much more often. And yes, I am old enough to have programmed
professionally in Fortran. There’s a reason we don’t use Fortran’s
least-favored features anymore, no matter how good it was in numerical
programming, and it’s not because of the tyranny of C.

Sorry, real life trumps dogma.


-john

February 28 1997: Last day libraries could order catalogue cards
from the Library of Congress.

Raphael G. wrote:

The pebble in the Ruby shoe is the counter-intuitive use of indexing of
elements of arrays, strings, etc., from 0 to n-1 instead of the more
natural 1 to n. Like prisoners who have got used to their chains, the
Ruby community seems to have meekly accepted this impediment to clear
thinking. It is particularly puzzling that Ruby should hobble its
users in this way, because it is manifestly unnecessary (e.g., Fortran
and Pascal don’t do it).

I sympathize, but if you were more experienced, I believe you would
understand it. Ruby does it the “right way.”

No flames intended, but I am certain you are not from a computer science
background?

Hal

On 19/07/07 John M. Gamble said:

Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.

Well, if one were to blindly follow one’s predecessors, yes - at least
with Fortran. Pascal lets you determine the array range, so you could
have the option of starting with zero. And indeed, when we first
encountered that possibility, zero got used a lot.

I believe that Fortran also prevents recursion. Perhaps we should dump
that
silly feature of Ruby too. :wink:

Mike

On 7/18/07, [email protected] [email protected] wrote:

for the whole, “0 is true” thing. :wink:
x||=42
x → false

But x ||= 1 is really: x = x || 1.
David I know that but it could be
x= 1 if x.nil?
Actually I think this might better reflect the common use cases, but
not be consistent (I said it again, sorry) with &&=, += etc, and that
probably would hurt too much.
And for that reason I was thinking about
x ??= 42.

That was the idea, sorry if I am not clear at all today :frowning:

So you’d end up with,
essentially, false || 1 being false, which wouldn’t make sense, and
sort of would be false evaluating to true :slight_smile:
Why not, just kidding:)

Robert

SonOfLilit [email protected] writes:

I disagree. Zero based arrays are natural to a mathematician, and
programmers tend to be mathematicians.

No! This is a systematic lie used to defend a convention that was
adopted for purely practical reasons. It’s like claiming that
segmented memory is more natural to mathematicians. Mathematicians
and even Computer Scientists consistently use 1, e.g.

x_1, x_2, …, x_n

The 0 (“not”) subscript–when used–usually shows up in situations not
really analogous to arrays, in order to INCREASE synchronization with
units, instead of decreasing it as 0-based arrays do (t_0 as the
instant of starting, t_1 the instant of 1 time unit from t_0, etc.)

Similarly, a[0] is NOT the “zeroth” (“Coming next in a series before
the one conventionally regarded as the first”) element!

It’s humanity that has the wrong clue. I hate the problems that
one-based counting creates. Anywhere, not just in arrays.

Aur

There are always trade-offs.

It’s certainly the case that the decision is made for Ruby. It’s a
good and safe decision supported by decades of computing convention
and habit, if not mathematical convention.

(Configurable array indexing is an abomination created by people who
must have thought that only a single author and the machine have any
business reading a program.)

Steve

P.S. I’m talking about U.S. English speaking mathematicians, since
that’s my only direct experience.

P.P.S. I must say, you’ve either worked with a much higher class of
programmers, or lower class of mathematicians than I have though.

Steven L. wrote:

SonOfLilit [email protected] writes:

I disagree. Zero based arrays are natural to a mathematician, and
programmers tend to be mathematicians.

No! This is a systematic lie used to defend a convention that was
adopted for purely practical reasons. It’s like claiming that
segmented memory is more natural to mathematicians. Mathematicians
and even Computer Scientists consistently use 1, e.g.

x_1, x_2, …, x_n

If you’re working in set theory, you get in the habit of 0 based
indexing, because then the index set is an ordinal number, i.e. ordinal
n = {0, …, n-1}.

In other branches (esp. algebra), it has the advantage that you can
treat the indexes as a cyclic group, do modular arithmetic, etc.

In general, 0 based indexing makes it easier to make the leap from a
list of variables to a function with a natural domain.

Michael P. Soulier wrote:

silly feature of Ruby too. :wink:
Historic FORTRAN was developed on hardware that made the use of arrays
in automatic story prohibitively expensive, so all historic FORTRAN
variables were static. Ergo, no recursion. But I know that automatic
variables came in in Fortran 77, and I’m pretty sure that recursion is
allowed now.

Michael P. Soulier wrote:

silly feature of Ruby too. :wink:

Mike

Well … they are talking seriously about dumping Call/CC. How many
Ruby programmers, or, for that matter, C programmers, actually use
recursion, even tail recursion? Recursion has been mainstream in
programming languages since Algol 60, but how many people actually use
it outside of Lisp/Scheme and other functional languages?

“Felix W.” [email protected] writes:

Here [0] a link to Dijkstra arguing why numbering should start at zero.

Regards,

Felix

[0] - E.W. Dijkstra Archive: Why numbering should start at zero (EWD 831)

When you write formal verification proofs for all your programs before
executing them, then quote Djikstra.

Anyway, this is a purely asthetic argument, the decision to use 0
follows only from his rejection of the interval notation (c), which he
explicitly rejects only for the case when there are no elements!

Steve

P.S. Ruby contains none of the candidate interval notations, though it
does have the “pernicious” dots, in two alternatives.

In message [email protected], “M. Edward (Ed) Borasky”
writes:

Well … they are talking seriously about dumping Call/CC. How many
Ruby programmers, or, for that matter, C programmers, actually use
recursion, even tail recursion? Recursion has been mainstream in
programming languages since Algol 60, but how many people actually use
it outside of Lisp/Scheme and other functional languages?

man qsort

-s

On Thu, Jul 19, 2007 at 09:10:22AM +0900, Steven L. wrote:

SonOfLilit [email protected] writes:

I disagree. Zero based arrays are natural to a mathematician, and
programmers tend to be mathematicians.

No! This is a systematic lie used to defend a convention that was
adopted for purely practical reasons. It’s like claiming that
segmented memory is more natural to mathematicians. Mathematicians
and even Computer Scientists consistently use 1, e.g.

Yes and no. I wouldn’t call it “systematic”, but it’s certainly a
falsehood, even if propagated largely by accident or with innocent
misunderstanding. Excepting in the sense that one can regard computer
science as an alternate and self-contained mathematics, quite distinct
from the mainstream “mathematics” field of study, programmers are not so
universally mathematicians as so many people seem so convinced they are.

The fact that mathematicians these days tend to end up being programmers
does not translate to programmers tending to be mathematicians. This is
not a one-to-one correspondence.

As you say, the zero-based array is a practical matter related to the
work of programming (as well as being related to the applicability of
set
theory to language design), and not a convenience for those wacky
mathematicians with their nonstandard way of counting (kind of a strange
claim to make). In contrast with your implication that this is somehow
a
bad practical decision, however, it’s actually the case that, by and
large, the benefits for natural sequential operations in programming and
the in-built compatibility of zero-based arrays with certain concepts of
set theory add up to a strikingly practical, and quite positive, reason
for zero-based arrays.

In other words, your premises are largely correct, but they do not lead
to the conclusions you seem to suggest.

Joel VanderWerf wrote:

If you’re working in set theory, you get in the habit of 0 based indexing,
because then the index set is an ordinal number, i.e. ordinal n = {0, …,
n-1}.

In other branches (esp. algebra), it has the advantage that you can treat
the indexes as a cyclic group, do modular arithmetic, etc.

In general, 0 based indexing makes it easier to make the leap from a list
of variables to a function with a natural domain.

Another way to say this is languages should lead programmers to think in
terms of set theory, not simplistic natural numbers, because the former
is
much more sustainable.

Imagine a language that used 1-based indexing. It would make working in
0-based appallingly hard even for simple situations. So we would prefer
this
slippery slope to slide in the correct direction.

Chad P. wrote:

Yes and no. I wouldn’t call it “systematic”, but it’s certainly a
falsehood, even if propagated largely by accident or with innocent
misunderstanding. Excepting in the sense that one can regard computer
science as an alternate and self-contained mathematics, quite distinct
from the mainstream “mathematics” field of study, programmers are not so
universally mathematicians as so many people seem so convinced they are.

The fact that mathematicians these days tend to end up being programmers
does not translate to programmers tending to be mathematicians. This is
not a one-to-one correspondence.

In fact, there is a school of thought that says computer scientists
shouldn’t be mathematicians! See

“Want to Be a Computer Scientist? Forget Maths” iTWire (07/05/07);
Corner, Stuart (http://www.itwire.com.au/content/view/13339/53/)

Of course, I think of myself as an applied mathematician, computer
scientist and software engineer as much as I think of myself as a
“programmer”. I personally think the argument at the above link is a
steaming pile, in other words. :slight_smile:

On 7/18/07, Phlip [email protected] wrote:

Imagine a language that used 1-based indexing. It would make working in
0-based appallingly hard even for simple situations. So we would prefer this
slippery slope to slide in the correct direction.

I’m a very young programmer, and I’ve known about this 0 vs 1 issue
for a while now. Phlip’s comment above is very good at explaining the
reason behind it, but I’ve had a hard time truly grasping it.

Is anyone willing to show some snippets of Ruby code which could
demonstrate the challenge of working with 1-based arrays? There were
some snippets in previous messages but they were too partial for me to
really understand things…