The Pebble in the Ruby Shoe

On Thu, Jul 19, 2007 at 01:43:36PM +0900, Sy Ali wrote:

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

On Thu, Jul 19, 2007 at 11:44:40AM +0900, M. Edward (Ed) Borasky wrote:

I believe that Fortran also prevents recursion. Perhaps we should dump that
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?

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.

Steven L. wrote:

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 U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-542, fax: +43 2236 21081
e-mail: [email protected]
Visit our Website: www.isis-papyrus.com

On 7/19/07, Peter S. [email protected] wrote:

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
There is no indication of recursion in the man page.

But I have nothing against recursion.

Robert

On 7/18/07, M. Edward (Ed) Borasky [email protected] wrote:

it outside of Lisp/Scheme and other functional languages?
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.

Logan C. wrote:

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.

(taken off-list since wandering way OT :slight_smile:

On 17 Jul 2007, at 20:03, Chad P. wrote:

On Tue, Jul 17, 2007 at 10:43:35PM +0900, Adrian H. wrote:

Perl’s misfeature is pretty different - it’s a global rather than a
per-array thing. Being able to say "this particular array has indices
-10 → 10 is considerably more useful than saying “every array starts
at -10” :slight_smile:

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

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

Cheers,

Adrian

On Thu, Jul 19, 2007 at 06:13:56PM +0900, Adrian H. wrote:

(taken off-list since wandering way OT :slight_smile:

Understandable.

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

I didn’t know about the likely awk connection in the existence of $[,
and
didn’t know early Perl used 1-indexing. I guess I learn something new
every day.

When I said something was news to me, however, I meant specifically your
statement that the “fix” was global rather than per-array. I somehow
read that as “it applies globally, and you can’t make it apply per
array”, rather than how I now realize you meant it. When you first
mentioned that, I thought you were saying there was something like
perhaps a pragma that changed the way Perl handled arrays, or something
like that.

. . . but yeah, I see now you were just referring to the fact that $[ is
a global variable. Of course, there’s always the local hack:

{
local $[ = 1;
# do stuff
}

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

would produce

6
5
4

Spooky action at a distance. Scary.

A little weird, to be sure – but awfully flexible if you need it, I
suppose. Probably the most disturbing thing about it for me is the fact
that, for each of the changes to $[ in your code snippet, that changes
the behavior of all arrays. Downright terrifying.

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

I don’t know any Fortran, so the talk of Fortran and arbitrary array
ranges is entirely new to me – and fascinating. The various
differences
of feature sets between languages really teaches some interesting
principles, I find.

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

Thanks.

On 7/19/07, M. Edward (Ed) Borasky [email protected] wrote:

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.

I see recursion as a useful tool of abstraction I remember having
implemented quick sort on a Sharp PC [forgot the name] in its
primitive Basic dialect. There were 13 cells left to be sorted LOL.
Never ever has a program given me more satisfaction, but nowadays I
would rather not waste my time on doing it manually, well I remember a
fascinating post of James explaining that unwinding the stack has
become second nature to him.
But who else will be able and/or willing to read your program?

Just my 2 nickels.

R.

On Jul 18, 2007, at 22:44 , M. Edward (Ed) Borasky wrote:

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?

I use recursion regularly, it’s a phenomenal way of supporting nested
hash processing (one example).

On Jul 18, 2007, at 20:10 , Steven L. wrote:

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.

I am a (US) Mathematician as are several of my close friends and we
all use 0 based indexing almost exclusively in our Mathematics work.
That said I do have to admit we all deal mainly in various branches
of Mathematical Logic.

Chad P. [email protected] writes:

Yes and no. I wouldn’t call it “systematic”, but it’s certainly a
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.

Not only did I not mean to imply that, but I’m not sure I can even
parse “bad practical decision”. If I knew the evidence I had implied
in support of my implied claim then maybe I could. :slight_smile:

But I even explicitly said that “It’s a good and safe decision
supported by decades of computing convention and habit…”

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

I’m just sick of hearing that Ruby does such and such because
mathematicians like it.

Steve

On Jul 18, 2007, at 23:11 , Chad P. wrote:

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.

I have had much first hand experience with programmers attempting to
do Mathematics and I can honestly say it is a very gory sight for the
majority of the attempts that I witnessed.

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.

I couldn’t agree more.

“Wayne E. Seguin” [email protected] writes:

of Mathematical Logic.
I stand by my claim in spite of a pair of anecdotal reports. Don’t
make me survey your papers. :slight_smile: (Actually, I can’t find many, if any,
publications for either of you. I’m not saying that means anything.)

Steve

P.S. If this is you then I suspect we may have mutual acquaintances:

On Fri, Jul 20, 2007 at 03:46:11AM +0900, Steven L. wrote:

set theory add up to a strikingly practical, and quite positive, reason
for zero-based arrays.

Not only did I not mean to imply that, but I’m not sure I can even
parse “bad practical decision”. If I knew the evidence I had implied
in support of my implied claim then maybe I could. :slight_smile:

But I even explicitly said that “It’s a good and safe decision
supported by decades of computing convention and habit…”

That would be why I used terms like “seem” and so on in that email – I
wasn’t 100% certain of your intent, based on what you said.

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

I’m just sick of hearing that Ruby does such and such because
mathematicians like it.

Fair enough. I agree with you on that score, actually – that it gets
kind of old seeing people try to equate everything in computer science
with the will of mathematicians. That’s the sort of thinking that leads
to CS students wasting time on trigonometry when they could be spending
it on something useful to computer science, like linear algebra.

On Jul 19, 2007, at 2:19 PM, Steven L. wrote:

That said I do have to admit we all deal mainly in various branches

This whole business about 1 or 0 based indexing is not important.
It’s a moot point.
If you want it added to the language, ask at Ruby Core, Matz does
read this stuff and does care what users think.
But if it doesn’t change, don’t waste time or electrons complaining
and arguing about it.
I’d like a lot of languages to change (CSS is one) but if you really
want it, make it.
That’s what Matz did!
Larry Wall made Perl, Guido Van Rossum made Python, and that guy who
made PHP.
Many of the more advanced, more esoteric languages were started as
the efforts of a single person.

On 7/19/07, M. Edward (Ed) Borasky [email protected] wrote:

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.

Interesting. This is wild speculation of course, but I imagine you are
probably atypical as far as always using explicit stacks. (Since I get
the
impression from your post history that you are heavily focused on
performance.). I would guess (more speculation ;)) that if someone were
to
grab a set of programs that for instance walked an XML DOM (or a similar
day
to day tree structure, like a nested directory) that the vast majority
of
those programs would use recursion with implicit stacks. Now that I’ve
contributed that bit of unsubstantiated nonsense, I’ll resume eating my
dinner. :slight_smile:

M. Edward (Ed) Borasky 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?

I used it just the other day in a Ruby solver for
URL:http://xkcd.com/c287.html (I should add that the last time I even
/thought/ about a packing problem was 1967, so my approach was brute
force.)

On Mon, 16 Jul 2007 22:22:05 +0900, 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).

If you were going to argue for arbitrarily indexed arrays e.g. those
ranging from 47 thru 300 or from -26 thru 68, you might have a point
that
there’s something more natural in your proposal. (Although it would
break
other features of arrays that we hold very dear to our hearts.)

Arguing 1-based arrays to replace 0-based arrays is arbitrary at best,
and unnatural at worst.

On Wed, 18 Jul 2007 22:45:34 +0900, Michael P. Soulier wrote:

On 18/07/07 Robert D. said:

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

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:

But in the shell, zero is the ONLY return code which is true.

–Ken