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).
--
Posted via http://www.ruby-forum.com/.
It's humanity that has the wrong clue. I hate the problems that
one-based counting creates. Anywhere, not just in arrays.
Aur
It's probably a matter of taste: I'm so used to indices starting at 0
that I would probably create a lot of bugs if I had to use arrays with
indices starting at 1.
For me an index has simply become a distance from the first element of
an array.
Anyway changing this would probably break at least 99% of all Ruby code
out there, so it's not likely to happen, is it?
Lionel.
Well now would be a hell of a time to change it, wouldn't it? =)
I, for one absolutely hate 1 based arrays in programming languages.
It creates a thousand little problems that one has to program around.
Ruby got it Right (again).
Well, why don't you make a new class that uses a hash as an array and
set the range to be from 1..X? A really cool thing about Ruby is that
making such things is simple and encouraged.
Dear Raphael,
from http://www.americanscientist.org/template/AssetDetail/assetid/51982?&print=yes,
an overview article about the history of around 6000 computer programming
languages:
"Still another perennially contentious issue is how to count. This one brings out the snarling dogmatism in the meekest programmer. Suppose we have a list of three items. Do we number them 1, 2, 3, or should it be 0, 1, 2? Everyone in computerdom knows the answer to that question, and knows it as an eternal truth held with the deepest, visceral conviction. Only one of the alternatives is logically tenable. But which is it ?.."
If you strongly believe that Ruby gets it all wrong, you can do all
the iterations with
Array.each{|item| ... } or
Array.each_with_index{|item,i| ... } # <= don't need to know where
you start counting actually
> > 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 check out different wordings of Peano's axioms, which define
the natural numbers, you'll find that they'll work regardless of whether
you name the first element 0 or 1 or 175758, so it's not necessary
to start at 0 or at 1 or anywhere else, but in the implementation
of a programming language, you sometimes have to make a convention choice.
Ruby, in contrast to some languages I used to work with before
(e.g., Matlab, which is a one-language, or C, which is a zero-language), lets you do your work even if you don't adhere to the convention choice made thanks to the each_with_index method.
Also, sometimes, you could prefer to count still differently:
e.g., January, February ,....
year.each{|month| p "Have a nice " + month }
I find that this can increase readability of a program a great deal...
Best regards,
Axel
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger
Every language does things the way the language designer wants to do it.
see this article for some discussion on it:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
AppleScript also uses 1 based arrays.
The practicality of any of several approaches is largely subjective.
Ideally there would be a flag to set the type of indexing you want to
use.
In Ruby, you could certainly do something to implement it, but it
would cast some overhead unless you created something like class
Array1 to be analogous to class Arrray, just indexed from 1 instead.
Feel free to create it and see if it would get added in.
It might certainly be useful as an option for some people, but in
general it is pretty simple to adjust between the paradigms.
Work the way a language works, don't try to make it work the way
another language works.
Many people, myself included, were first confounded by this in C, but
you adapt quickly.
0 indexing is convenient in that the element equal to the length is
out of bounds or contains a special character/marker to denote the
end of the array, depending on the language.
Some things are just continued because of convention. Most languages
use 0-based indexing.
Am Montag, 16. Jul 2007, 22:22:05 +0900 schrieb Raphael Gillett:
> 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.
To start indexing of arrays from 0 is any good programmers
-first- sorry zeroth lesson. It is the natural way. Get used
to it.
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Both ideas are completely stupid, there is only one err zero err 42 solutions.
42, 43, 44
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
Should array indices start at 0 or 1? My compromise of 0.5 was
rejected without, I thought, proper consideration. -- Stan
Kelly-Bootle
martin
> 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).
and we all know how 'natural' *those* languages are do program in
(ducks)...
a @ http://drawohara.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama
Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.
However, since it was implemented using C, Ruby follows the convention
of implementation.
And we all know how easy it is to program in C ;-)
Blessings,
TwP
I grew up with 0-based arrays. 1-based ones look unnatural to me. That's the
truth, as for me.
The worst nightmare for me was CHS addressing on disks - Cylinders are
0-based, Heads are 0-based, but Sectors are 1-based. Hell on earth.
That were my 3 cents.
PS.
O, and yes. Welcome everybody! I felt in love with Ruby 2 weeks ago. I find
it much more flexible than Python, my previous love and much more
expressive (and object-oriented) than PERL, my next-to-previous love. I
think Ruby puts the best of all scripting languages together. Gott sei dank
for Yukihiro Matsumoto and his precious mind.
--
Paweł Kraszewski
www.kraszewscy.net
I always thought it made more sense to index from zero. After all, an
index represents an offset from the start, and the rest of the aspect is
just a one to one correspondence. The first thing there could as easily
be item Skattlebraught, the second being item Porktail, etc. The
standard of C using 0 as an index happened because it made sense, not
because it was merely different. I think Matz has typically adopted
things that make the most sense, and though some may not be used to
this, I don't find it unclear and it is more rational than starting with
index 1, though it may be less popular among some people. I think we
are always better off considering the adoption of something non-standard
that might be better. After all, if not, we would never have gotten
beyond COBOL and JCL, and we might all be living some "Brazil" Orwellian
nightmare.
>
> Blessings,
> TwP
>
>
--
The only sustainable organizing methods focus not on scale,
but on good design of the functional unit,
not on winning battles, but on preservation.
Ok, I know that this is not a Ruby point, but I feel compelled to
respond. The basic array in Pascal is zero based.
var
ar: array of integer; // dynamic array
begin
SetLength(ar, 10); // makes a 0 - 9 array
end;
var
ar: array[1..10] of integer; // makes a 1 - 10 array
The default is pretty much zero based, even in Pascal.
> 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
But since Ruby lets you reach right in and override everything, couldn't
you just change this in your own programs if you don't like it?
m.
--
matt neuburg, phd = ma...@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
Hmm, would probably break the standard library.
But you could inherit from Array.
Aur
This seems like a terrible idea. Part of learning a language is learning
the ways that things are done in that language. If a coder starts
rewriting basic methods because of some silly prejudice against
zero-based indices, then that coder has just introduced a huge potential
trap for anyone wanting to modify that code. Some seriously nasty bugs
would almost certainly be introduced until the contributor realizes that
their basic assumptions about the language have been changed. Rewriting
functionality at that level should always be done with a very careful
analysis of how those changes might impact other coders, and the code
that other coders write.
Think before you reimplement.
Tom
On many disks, there is (or used to be) a sector #0 that is not used for
data; instead, it is used to record information about bad sectors, or
similar things.
--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
> 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).
Anyone who has serious difficulty with 0 based arrays should consider
other employment. Food waiting, bar tending or similar would be a
better choice.
I wouldn't go quite that far, but I have to say, I don't think 0-based
indexing is at ALL a bad thing. I agree that it's arguably not perfectly
intuitive, but... Well, that's true of a lot of things, and sometimes
that's because our intuition doesn't work very well.
-s
Well.. I do enough testing that I usually catch this oversight.. urr
undersight and work around it. I try to accept it because I'm no
mathematician, it's not my call to make and I'm a regular person and
not a real programmer so I don't matter.. but secretly I want to punch
supporters 0 times in the nose and kick them 0 times in the nuts.
I hope I'll get used to 0-based arrays one day. Urr.. zero day.
;)
For the comedy-deficient, that was comedy.
It's still unnerving to work with a 0-based array, because while I
begin counting with zero.. I don't attach any content to that
empty-count. For me, to say there are 0 items is to not begin
counting. It's as though a piece of me expects the 0 in an array to
be nil and for it to never house useful data. I'll get over it
though.
And to argue that rewriting things "is impossible now" isn't quite
correct. It's impossible _now_ but it's possible for Ruby 2.0 isn't
it? Crazy, but possible.
The pebble is a ruby :-)
Sent from my iPhone
On Jul 17, 2007, at 12:00 AM, Peña, Botp <bo...@delmonte-phil.com>
wrote:
Religion, wannabe game programmers, and now this! The list is driving me
nuts this past week.
Pete Yandell
http://notahat.com/
Is this some kind of troll?
My own experience with programming makes me feel that counting arrays
from 0 is the natural thing. If you are handling an array 5 items
starting from x, going from x+0 to x+4 is more concise than going from
x+1-1 to x+5-1. There are other examples in which I have found it
convenient.
Chinese count birthdays starting with the child's first year as year
1. As a westerner do you consider it natural describe a child as 2
years old on its "first" birthday?
It may be counterintuitive but in the mathematical world it is quite natural.
But maybe some learned francophone mathematician can mail me off list,
I am only working with engineers, natural as an engineer, you know ;)
Cheers
As a former bartender, I take umbrage at this remark. We all used zero
based indexing.
> As a former bartender, I take umbrage at this remark. We all used zero
> based indexing.
Ah please I'd like half a glass #{of_whatever_liquid}, but the upper
half of the glass.
I'd probably not dare asking that in any bar.
Gosh I am OT,OT,OT, sorry folks nobody else to talk to ;)
>
>
>
R
The avalanche has already started. It is too late for the pebbles to vote.
--
Florian Frank
> Tim Pease wrote:
>> Actually, if Ruby had been implemented in Fortran or Pascal, then
>> most
>> certainly arrays would have begun indexing with 1 instead of 0.
>
> Ok, I know that this is not a Ruby point, but I feel compelled to
> respond. The basic array in Pascal is zero based.
[snip]
Fortran doesn't care much either. There's a convention for starting
at 1, but the language allows you to specify an arbitrary index range
for your arrays.
Adrian
Perl copied that misfeature, though it defaults to zero, in the hopes
of attracting Fortran programmers. What they learned was that it
breaks way too much. I'm pretty sure it's being removed in Perl 6.
James Edward Gray II
type my_array = array[<somerange>] of <sometype>;
It was up to you, right? Maybe you are talking of some dialects or did
I really forget??
> [snip]
<snip>
> On Jul 17, 2007, at 7:58 AM, Adrian Howard wrote:
>
>>
>> On 16 Jul 2007, at 19:22, Lloyd Linklater wrote:
>>
>>> Tim Pease wrote:
>>>> Actually, if Ruby had been implemented in Fortran or Pascal,
>>>> then most
>>>> certainly arrays would have begun indexing with 1 instead of 0.
>>>
>>> Ok, I know that this is not a Ruby point, but I feel compelled to
>>> respond. The basic array in Pascal is zero based.
>> [snip]
>>
>> Fortran doesn't care much either. There's a convention for
>> starting at 1, but the language allows you to specify an arbitrary
>> index range for your arrays.
>
> Perl copied that misfeature, though it defaults to zero, in the
> hopes of attracting Fortran programmers.
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" :-)
> What they learned was that it breaks way too much. I'm pretty
> sure it's being removed in Perl 6.
It is. Everybody admits it was a bad idea.
Adrian
On Jul 17, 8:27 am, James Edward Gray II <ja...@grayproductions.net>
wrote:
I spent plenty of time trying to explain to people the difference
between using an array in scalar context (to get the number of
elements) and using the "end of array" sigil (to get the last index).
I don't recall seeing people check $#array against -1 to see if the
array was empty, but I remember plenty of iterating over 0 .. @array -
1.
Of course, never did I (seriously) tell people to use $[ .. $#array.
That would work no matter what, but I preferred to think of it as a
test case. If some module I'm using changed array indexing, let me
know. I'll never use it again.
--
-yossef
--
-yossef
I should go look that up. :)
I never used ALGOL, but:
http://en.wikipedia.org/wiki/ALGOL_58
Both IAL and ALGOL 60 allowed arrays with arbitrary lower and upper
subscript bounds, and allowed subscript bounds to be defined by
integer expressions.
http://www.cs.virginia.edu/~mpw7t/cs655/pos2.html
ALGOL 68 was designed to be an improvement upon ALGOL 60. In ALGOL 60,
arrays could be multi-dimensional, they could be dynamically sized,
and their lower bound index could be non-zero [1].
> The avalanche has already started. It is too late for the pebbles to
> vote.
Oh my Kosh!
News to me. Would you point me at more information on the topic, please?
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Ben Franklin: "As we enjoy great Advantages from the Inventions of others
we should be glad of an Opportunity to serve others by any Invention of
ours, and this we should do freely and generously."
I wonder how C got saddled with a fixed lower limit of 0 then. It must
have been a simplification somewhere in the chain from Algol - CPL -
BCPL - B - C.
> http://www.cs.virginia.edu/~mpw7t/cs655/pos2.html
> ALGOL 68 was designed to be an improvement upon ALGOL 60. In ALGOL 60,
> arrays could be multi-dimensional, they could be dynamically sized,
> and their lower bound index could be non-zero [1].
Algol 68 was a truly marvelous language that somehow never caught on.
In C the expression array[n] is actually just syntactic sugar for
*(array + n). Literally. I haven't tried in years, but the following
is supposed to be legitimate: n[array]. Great for obfuscating code.
If that's how you're going to perform array lookups, then your array
indices have to start from 0.
Giles
> 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.
With 50+ years of collective experience, it seems clear that zero-
based arrays lead to the simplest code.
This indeed works, and consequently funny variations such as
(n1-n2)[n2+array] work too. It is obfuscating not so much because
it were not easy to understand, but because we simply are not used to
see it written that way (because other languages usually do it in
the other way around).
To contrast with, the IMO most unpleasant way of approaching the index
origin was done in APL. There, you could set the system variable _IO
to either 0 or 1 (no other value accepted), which rules whether array
the index of *any* array is from now on counted from 0 or from 1.
True, you *could* localize this setting in a function, but at least
in original APL (I don't know about APL2), these were not truly
"local" variables like in Ruby, but more like those "local global"
variables you would for example declare in Perl by writing
'local $v=....;', with the consequence that they the change to _IO
would be visible in all functions called from within the function
which had "locally" reset the index origin. This has the consequence
that if you write *any* reusable function for APL (such as a library),
you must use the value of this variable for indexing. For example,
in order to access the Nth element of an array A, you would write
something like A[N+_IO-1].
BTW: Though I - like most programmers - find it most convenient counting
arrays from zero, there are cases where I would find it more natural
if I could choose the upper and lower bound of an array by myself (as
it is done in languages such as Ada). Such an approach would work well
in a statically typed language (where you can in principle look up
the variable declaration), but would make understanding a program
unnecessarily hard in dynamic languages such as Ruby.
Ronald
Perl has this too, though it's called $[.
Obviously, it isn't normally used and people assume the default value of
0. :)
-mental
That's been my personal experience, too -- pretty much the only thing
1-based indices make simpler is getting the last element of an array
given its size; everything else seems to get more complex.
-mental
True! It seems that the Perl community (reasonably) assumes
that whoever fiddles with $[, so everyone assumes it to be
0, while in the APL world it was more common to expect that everyone
might change _IO...
Ronald
> I do not like categorical statements, but this one might be the
> exception that confirms the rule.
> +1 (or was that +0 ;)
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. ;-)
Mike
--
Michael P. Soulier <msou...@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein
Assembly language programming, at least on binary machines, is a lot
more natural with 0-based indexing, since that's the way the hardware is
laid out.
I haven't forgiven Scheme for "nil is true" either!! That's just *so*
wrong to a Lisp 1.5 programmer.
R.
...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.
--
John W. Kennedy
"The bright critics assembled in this volume will doubtless show, in
their sophisticated and ingenious new ways, that, just as /Pooh/ is
suffused with humanism, our humanism itself, at this late date, has
become full of /Pooh./"
-- Frederick Crews. "Postmodern Pooh", Preface
> Assembly language programming, at least on binary machines,
...all the decimal machines I can recall, too...
> is a lot
> more natural with 0-based indexing, since that's the way the hardware is
> laid out.
PL/I implementations have historically regarded the address of an array
as being where the (0, 0, 0...) element would be, even if one doesn't
exist, to reconcile these two.
--
John W. Kennedy
A proud member of the reality-based community.
but but but...
The original idea of zero as true, if memory serves, is that a function
can return a result based on whatever it was doing. It was assumed that
there was one way to get everything to run right but many ways to fail.
SO, if you have zero as the true and everything else as both an
pass/fail indicator AND telling you just what the problem was, then true
= zero makes a good deal of sense.
my $0.02
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
x=false
x||=42
x --> false
or even
x ??= 42
for that semantics.
> 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?
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Amazon.com interview candidate: "When C++ is your hammer, everything starts
to look like your thumb."
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Dr. Ron Paul: "Liberty has meaning only if we still believe in it when
terrible things happen and a false government security blanket beckons."
On Thu, 19 Jul 2007, Robert Dober wrote:
> On 7/18/07, MenTaLguY <men...@rydia.net> wrote:
>> On Wed, 2007-07-18 at 22:56 +0900, Robert Dober 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. ;-)
>> > 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
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)
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:)
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.
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
> >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. ;-)
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.
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
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.
--
John W. Kennedy
"...if you had to fall in love with someone who was evil, I can see why
it was her."
-- "Alias"
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
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Marvin Minsky: "It's just incredible that a trillion-synapse computer could
actually spend Saturday afternoon watching a football game."
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
awj @reddit: "The terms never and always are never always true."
> 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.
--
Phlip
http://www.oreilly.com/catalog/9780596510657/
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax
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. :)
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
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
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
John Kenneth Galbraith: "If all else fails, immortality can always be
assured through spectacular error."
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: micha...@isis-papyrus.com
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.
---------------------------------------------------------------
> (taken off-list since wandering way OT :-)
[snip]
Or it would have been if I'd had more coffee this morning - sigh -
apologies all.
Adrian
On 17 Jul 2007, at 20:03, Chad Perrin wrote:
> On Tue, Jul 17, 2007 at 10:43:35PM +0900, Adrian Howard 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" :-)
>
> 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
But I have nothing against recursion.
Robert
>
> -s
> 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.
>
Just my 2 nickels.
R.
Understandable.
>
> On 17 Jul 2007, at 20:03, Chad Perrin wrote:
>
> >On Tue, Jul 17, 2007 at 10:43:35PM +0900, Adrian Howard 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" :-)
> >
> >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
}
>
> 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.
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 :-)
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
. . and I replied without checking the address. Heh. I guess I helped
propagate the mistake. Ouch.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
> "Wayne E. Seguin" <waynee...@gmail.com> writes:
>> On Jul 18, 2007, at 20:10 , Steven Lumos 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.
>>> <snip>
>>
>> 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.
>
> I stand by my claim in spite of a pair of anecdotal reports. Don't
> make me survey your papers. :-) (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:
> http://www.google.com/patents?hl=en&lr=&vid=USPAT5885158
>
>
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.
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.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Larry Wall: "A script is what you give the actors. A program is what you
give the audience."
> 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).
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.
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/
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.)
--
John W. Kennedy
Read the remains of Shakespeare's lost play, now annotated!
http://pws.prserv.net/jwkennedy/Double%20Falshood/index.html
But I agree these are rare exceptions and no reason at all not to use
recursion, a better design might have allowed for a recursive solution
too :)
Cheers
Robert
> On 18/07/07 Robert Dober said:
>
>> I do not like categorical statements, but this one might be the
>> exception that confirms the rule.
>> +1 (or was that +0 ;)
>
> 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. ;-)
But in the shell, zero is the ONLY return code which is true.
--Ken
I was actually one of the first students in the USA to get calculus and
analytic geometry in high school. This was right after Sputnik and I
lived in a town full of scientists and engineers. I was also fortunate
to have graduated before the "New Math", which was mostly set theory.
So ... would you say queuing theory is useful to computer science? I
certainly would. Well, it turns out, as we mathematicians like to say,
that you can use a Fast Fourier Transform to solve some queuing theory
problems that are more or less intractable otherwise. Can you do a Fast
Fourier Transform without sines and cosines?
1. I am the very model of premature optimization. Unless the underlying
run-time has made a great effort to make (tail) recursion efficient,
like most Lisps, Schemes, and the core of Derive, it's usually less
efficient than iteration and harder for other programmers to understand.
2. I've been quite fortunate in avoiding XML so far. I like *my* trees
with squirrels and blue jays in them, thankyouverymuch.
3. Perhaps Charles Nutter would comment on how efficient jRuby on the
JVM is at recursive code compared to MRI Ruby. I'd expect it to be
better, given that some Schemers were involved in the creation of Java. :)
>
There's a pretty big difference between "electrical engineering" and
"computer science".
>
> I was actually one of the first students in the USA to get calculus and
> analytic geometry in high school. This was right after Sputnik and I
> lived in a town full of scientists and engineers. I was also fortunate
> to have graduated before the "New Math", which was mostly set theory.
>
> So ... would you say queuing theory is useful to computer science? I
> certainly would. Well, it turns out, as we mathematicians like to say,
> that you can use a Fast Fourier Transform to solve some queuing theory
> problems that are more or less intractable otherwise. Can you do a Fast
> Fourier Transform without sines and cosines?
Maybe not -- but my objection is more to the saddling of CS students with
several years of trig and calculus *classes* than with the imparting of
any of the basics that are related to those subjects. The basics of
trigonometry as taught in a trig class, in fact, can be imparted to a
student quite thoroughly in about two class sessions -- and it usually
is, in a preceding geometry or algebra course. Meanwhile, the rest of
what's "taught" in typical college trig courses can be easily derived
from the basics, and is just rote memorization for the purpose of passing
tests, to be forgotten immediately after the test in time to memorize for
the next exam.
Meh. I have traumatic experiences of almost being entirely turned off of
"higher education" by the stupidities of a computer science curriculum.
Your mileage may vary.
--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Baltasar Gracian: "A wise man gets more from his enemies than a fool from
his friends."
On Jul 19, 3:19 pm, Steven Lumos <ste...@lumos.us> wrote:
> I stand by my claim in spite of a pair of anecdotal reports. Don't
> make me survey your papers. :-) (Actually, I can't find many, if any,
> publications for either of you. I'm not saying that means anything.)
I haven't had /that many/ opportunities to use recursion, but I tend
to use it wherever it is both applicable and not prohibitively
resource intensive. The latter is a valid concern when choosing how to
solve a given problem, but for cases in which recursion won't blow the
stack or drastically affect performance, I like to use it. Not because
it's /better/, per se...just because it's more fun and often a bit
more challenging.
Granted, I'm still a student in the early part of my education, so I
don't know how viable recursion will be for me in "the real world", in
consumer apps and the like, but I'll still use it when I see an
opportunity.