The Pebble in the Ruby Shoe

C

Chad Perrin

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

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.
 
C

Chad Perrin

...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.
 
D

dblack

Hi --

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 :)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober

Hi --



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 :(
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 :)
Why not, just kidding:)

Robert
 
J

John M. Gamble

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.
 
H

Hal Fulton

Raphael said:
Ruby aims to be a human friendly programming language that embodies the
principle of least surprise. However, there is an important feature of
the language that, despite being a glaring exception to this laudable
goal, seems to have crept unquestioned into the Ruby core. It is a
rebarbative and truly medieval practice which everyone knows causes
endless confusion, countless unnecessary errors, and a great deal of
wasted programming time in all languages that incorporate it, e.g., C,
Java. In violation of Ruby's ethos, this feature is present purely to
suit the compiler/interpreter at the expense of the person.

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
 
M

Michael P. Soulier

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. ;-)

Mike
 
J

Joel VanderWerf

Steven said:
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.
 
J

John W. Kennedy

Michael said:
I believe that Fortran also prevents recursion. Perhaps we should dump that
silly feature of Ruby too. ;-)

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.
 
M

M. Edward (Ed) Borasky

Michael said:
I believe that Fortran also prevents recursion. Perhaps we should dump that
silly feature of Ruby too. ;-)

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?
 
C

Chad Perrin

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?

Me, for one. It's not very common, but . . . once in a while, I do. Not
in Ruby yet, other than to play around, but in Perl a bit -- when it's
appropriate.
 
C

Chad Perrin

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.
 
M

M. Edward (Ed) Borasky

Chad said:
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. :)
 
S

Sy Ali

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..
 
C

Chad Perrin

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..
irb(main):001:0> foo = %w[zero one two three]
=> ["zero", "one", "two", "three"]
irb(main):002:0> puts "last element: #{foo[-1]}"
last element: three
=> nil
 
M

Michael Ulm

Steven said:
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

I've worked for over 10 years as a mathematician on a university
level. I've met and worked with mathematicians from all continents
except Antarctica and from more nations that I care to count.
So, I think I speak with some authority when I say, that there is a
definitive consensus amongst a vast majority of mathematicians
concerning this issue, and this is that they don't care all that
much.

For a mathematician, the start of a sequence isn't that interesting
anyway. They much more care about the other end at infinity.
Personally, when doing mathematics my sequences start with x_0,
unless they don't.

Regards,

Michael


--
Michael Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-542, fax: +43 2236 21081
e-mail: (e-mail address removed)
Visit our Website: www.isis-papyrus.com

---------------------------------------------------------------
This e-mail is only intended for the recipient and not legally
binding. Unauthorised use, publication, reproduction or
disclosure of the content of this e-mail is not permitted.
This email has been checked for known viruses, but ISIS accepts
no responsibility for malicious or inappropriate content.
---------------------------------------------------------------
 
A

Adrian Howard

(taken off-list since wandering way OT :)

News to me. Would you point me at more information on the topic,
please?

In the perl world we have the $[ variable (see perldoc perlvar for
gory details) that resets the index of the first element. As the
documentation says "Its use is highly discouraged.". I'd imagine that
it's primary motivation for inclusion was to make compatability with
awk easier in early perl's (which also has 1 indexing).

For example:

my @array = ( 1 .. 10 );

$[ = 0;
print @array[5], "\n";

$[ = 1;
print @array[5], "\n";

$[ = 2;
print @array[5], "\n";

would produce

6
5
4

Spooky action at a distance. Scary.

Fortran (or those versions that support it anyway) allow you to
specify the indices of a particular array - so for example you could
have

real temperature(-100:100)

to make an array of real values with indices from -100 to 100. So you
can make the indices of your array match the indices of your domain -
if that would make your life easier :)

See <http://www.stanford.edu/class/me200c/tutorial_77/10_arrays.html>
for some examples.

Cheers,

Adrian
 
M

M. Edward (Ed) Borasky

Logan said:
You've never written code to traverse some kind of tree? And if you have,
you did it all iteratively? I would suggest _lots_ of programmers use
recursion, even in C.

There's a big difference between an explicit stack with iterative code
and recursion with an implicit stack. I've done a lot of explicit stack
coding for walking data structures -- indeed, the last time I did so was
a recent piece of code that generates the state space of a large finite
Markov chain. But the only time I've written recursive functions as a
*preferred* modus operandi has been in Lisp, Scheme and the Lisp-based
symbolic math package Derive. And there, I've always aimed for tail
recursion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,314
Members
47,993
Latest member
Meri511146

Latest Threads

Top