13 ms·
One thing I would hope there is consensus on is that 1-based indexing is easier to learn. My son is doing Python at school, and the "first-element-is-actually-e
by tragomaskhalos 6y ago
One thing I would hope there is consensus on is that 1-based indexing is easier to learn. My son is doing Python at school, and the "first-element-is-actually-element-zero" is something that has to be dinned into them, a sure sign that it is non-intuitive. In similar vein, as adult programmers we know that the half-open range is the most useful mechanism for expressing a sequence, but again we have to explain to kids that if they want the numbers 1 to 12 (eg to do a times table program) they must type range(1,13), which at that stage of their learning just seems bizarre. Actually I could go on at length about why Python is a terrible teaching language, but I'll stop there !
- dragonwriter 6y agoHonestly, the problem is that we don't distinguish cardinal from ordinal numbers in programming As an offset (which is cardinal—how far something is from a starting point) or really any cardinal use, zero is the natural starring point. For ordinals, “1st” is the natural starring point. But we don't write “1st” in programming, even when we used 1-based indexing to try to match an ordinal intuition, we just write “1”. If we distiguished ordinals and allowed both cardinal offsets and ordinal indexes to be used, I think we’d be fine.
- adrian_b 6y ago"1st" is not a "natural" choice for the starting point of ordinal numbers. If anything, it is an artificial choice, because in programming it is derived from a property of most, probably of all, human languages that have ordinal numerals. In most, probably in all, human languages the ordinal numerals are derived from the cardinal numerals through the intermediate of the counting sequence 1, 2, 3, ... When cardinal numbers appeared, their initial use was only to communicate how many elements are in a set, which was established by counting 1, 2, 3, ... Later people realized that they can refer to an element of a sequence by using the number reached at that element when counting the elements of the sequence, so the ordinal numerals appeared, being derived from the cardinals by applying some modifier. So any discussion about whether 1 is more natural than 0 as the starting index goes back to whether 1 is more natural as the starting point of the counting sequence. All human languages have words for expressing zero as the number of elements of a set, but the speakers of ancient languages did not consider 0 as a number, mainly because it was not obtained by counting. There was no need to count the elements of an empty set, you just looked at it and it was obvious that the number was 0. Counting with the traditional sequence can be interpreted as looking at the sequence in front of you, pointing at the right of an element and saying how many elements are at the left of your hand, then moving your hand one position to the right. It is equally possible to count by looking at the sequence in front of you, pointing at the left of an element and saying how many elements are at the left of your hand, then moving your hand one position to the right. In the second variant, the counting sequence becomes 0, 1, 2, 3, ... The human languages do not use the second variant for 2 reasons, one reason is that zero was not perceived as having the same nature as the other cardinal numbers and the other reason is that the second variant has 1 extra step, which is not needed, because when looking at a set with 1 element, it is obvious that the number of elements is 1, without counting. So neither 0 or 1 is a more "natural" choice for starting counting, but 1 is more economical when the counting is done by humans. When the counting is done by machines, 0 as the starting point is slightly more economical, because it can be simpler to initialize all the bits or digits of an electronic or mechanical counter to the same value for 0, than initializing them to different values, for 1. While 1 was a more economical choice for humans counting sheep, 0 is a choice that is always slightly simpler, both for hardware and software implementations and for programmers, who are less likely to do "off by one" errors when using 0-based indices, because many index-computing formulas, especially in multi-dimensional arrays, become a little simpler. In conclusion, the choice between 0 and 1 never has anything to do with "naturalness", but it should always be based on efficiency and simplicity. I prefer 0, even if I have programmed for many years in 1-based languages, like Fortran & Basic, before first using the 0-based C and the other languages influenced by it.
- scambier 6y ago> whether 1 is more natural as the starting point of the counting sequence. Seriously? - "Hey Timmy, how many pens do you have on your desk?" - "Let's count them! Zero, One, Two." - "So how many is that?" - "Well since I ended my counting sequence with 'two', it totally makes sense to announce that I have three pens."
- adrian_b 6y agoI did not explain in detail how a human should count, because I think that for a programmer it should have been clear that the exit criterion from the loop is in both cases to have no elements at the right of your hand. So your Timmy has a bug in his loop code, which caused a premature exit :-) He should have counted 0, 1, 2, 3. Like I have said, humans do not count like this, because there are 4 steps instead of 3 steps. For humans, it is easier to have an "if" before the loop, selecting between the special case of an empty set and a non-empty set, whose elements must be counted. Nonetheless, an electronic counter would have really counted 0, 1, 2, 3, unlike you.
- scambier 6y agoYou wrote a lengthy post trying to explain how it could totally be possible that humans start counting at zero, yet the whole premise of it is wrong: we start counting at 1 because it's natural. You cannot assume otherwise. > He should have counted 0, 1, 2, 3 Is that how _you_ count? You imagine some pointer going _between_ items, start at zero, and stop once you have nothing at the right of your pointer? Or, little Timmy could also skip the "zero" part (because you know, he's not dumb nor blind, and sees that he has several pens on his desk), start at 1 while pointing _at_ the first pen, and count up to 3.
- sdenton4 6y ago(And then, having pointed at the pens, Timmy got confused and tried to write down his answer using his finger. Annoyed that no actual writing occurred, he cut off his finger and threw it away. The next day, he was again confused to find his desk all cluttered with crap that somewhat resembled pens, despite having so bloodily thrown away his pointer.)
- pwdisswordfish5 6y agoBut zero is an https://en.wikipedia.org/wiki/Ordinal_number_(mathematics) https://en.wikipedia.org/wiki/Ordinal_number_(mathematics) . The unintuitive step is apparently identifying the index with the cardinality of the collection from before the item arrives, not after it arrives – i.e. ‘the n-th element is the one that arrived after I had n elements’, not ‘the n-th element is the one such that I had n elements after it arrived’. The former identification results in 0-based ordinals, the latter leads to 1-based ordinals – and to some misconceptions about infinity, such as imagining an element ‘at index infinity’ in an infinite list, where no such need to exist.
- pegasus 6y agoIt's only an ordinal because we've confused ourselves into saying it can be. It's the same situation as with starting to count from zero in most programming languages, and from the wikipedia page for "0th", it appears to actually be the effect of an influence of those artificial language choices onto the mathematical concept of ordinal. After all, it is in the end just a convention, and we could start calling the first element of a sequence "the zeroth" instead, but there would be no benefit for it at the language level - which is where it matters. These artificial languages we're creating should try to mold themselves to how we already use language, but the opposte is what's been happening to some degree.
- nvader 6y agohttps://danverbraganza.com/writings/zero-based-ordinals https://danverbraganza.com/writings/zero-based-ordinals ;)
- sergeykish 6y agoFirst element found at offset 0. Like ruler. Closed and right-open intervals in Ruby: (1..12).to_a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] (1...13).to_a #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
- erhk 6y agoI see myself losing hours on this typo
- ZephyrBlu 6y agoI couldn't tell the difference for quite a few seconds there.
- bregma 6y agoWe need a language that goes one better, like a cross between Python and Ruby that uses two spaces for closed ranges and three spaces for open.
- TuringTest 6y agoHow about using different symbols? |-1..12-|.to_a ## e.g. months in a year #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] /1..\13.to_a ## e.g. hours from 1h (1AM) to 13h (1PM) #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] It requires compiler support, but hey, you said a new language.
- sergeykish 6y agoI see children losing hours on range(1, 12) in Python, have you noticed error? Unsafe defaults introduced to reuse function as for i in range(len) # 0, 1, ..., len - 1
- stabbles 6y agoAnd Rust does 1..13 and 1..=12
- 6y ago
- bachmeier 6y agoYou're supposed to start out by teaching them pointers. Once they fully understand how pointers work, you move them on to Hello World so they can write their first program. Then it will be intuitive when they see 0-based indexing. I think that's the HN consensus on teaching kids how to program.
- djxfade 6y agoI don't think a lot of beginner friendly languages exposes pointers to the developer
- randomdata 6y agoUnless they have arrays, which are conceptually not a whole lot different.
- mewpmewp2 6y agoMost of the time you don't use them or think or need to think of them as pointers.
- randomdata 6y agoNo, but pointers are thought of as array indices. If you understand arrays, you understand pointers.
- mewpmewp2 6y agoWhat do you mean by understanding? E.g I have been using arrays productively in PHP and JavaScript, does it mean I understand them?
- randomdata 6y agoIf, given a[n], you understand that n points to a position in array a then you understand pointers.
- boxed 6y agoIt's only easier to learn because that's how we teach our kids. The first and most basic number is not one. It's zero. I am doing my best in teaching my kids this way but the world around has the wrong idea. We'll see how it goes...
- stabbles 6y agoThis might be an unpopular opinion, but I'd say it's more natural for kids to relate the word 'three' to 'third' and 'four' to 'fourth', instead of 'two' to 'third' and 'three' to 'fourth'.
- bregma 6y agoYes, that's why its hard to explain to kids that a two-year-old is in their third year of life. By the time of their third year of school (second grade) they usually get it, though. We should be careful not let such easily observed facts inhibit our efforts to dumb down curriculum.
- IncRnd 6y agoKids are taught units, so they express themselves properly and are understood by others. e.g. The third item over here is at a distance of two cm from the left edge.
- canadianfella 6y agoWhat is “dinned”?
- bregma 6y agoPeople have to learn something? That's terrible.
- lrossi 6y agoI think it’s even harder to wrap one’s head around negative indices in Python, and all the ways to do slicing. They should have added a reversed keyword instead of the current complexity.
- tremon 6y agoIt is non-intuitive, but it doesn't happen only in programming. Look at sports reporting, for example (basketball, football). When they say something happened "in the 6th minute", it happened between 05:00 and 05:59. The difficulty isn't solely with programming. You can use the same mechanism (minutes:seconds) to explain the half-open ranges: does a range(0,30) mean from 0:00 to 30:00 or from 0:00 to 30:59? If the second half of the match starts at 30:00, does the first half really end at 30:00 or is it really 29:59:99...9? For most basic concepts, there's always real-world examples to use so that the teaching doesn't have to be abstract.
- teekert 6y agoYeah and kids are actually aged "1" in their second year.
- improbable22 6y agoTime is a continuous variable, and kids know this pretty early on: they will happily tell you they are 5 and a half years old. Their second year starts when they are aged 1.0. English in general makes this distinction: "He is fewer than 6 years old" is simply a mistake, while "less than 6 years old" is fine. (I think some dialects use "fewer" less often, in general, but would be pretty surprised if any prefer it here.)
- AuthorizedCust 6y agoWhat we call “age” is merely the number of the most recent birth anniversary we’ve passed. That makes sense. While your take isn’t invalid, it sets up a less intuitive construct that isn’t aligned with the common meaning of the word.
- rmah 6y agoIn korea, they'd be age 2. There (and possibly other east asian nations?) count "age" in terms of which year you're in. 1st year, 2nd year, 3rd year, etc. The way americans do school year, except it starts at birth.
- 6y ago
- coldtea 6y ago>a sure sign that it is non-intuitive Well, 0-based is not what we use in everyday life, and even arriving at the invention of 0 took us several millenia, so I'd say its non-intuitiveness is pretty much established...
- teekert 6y agoWell, I was born aged 0, turn 1 at the start of my second year on the planet.
- improbable22 6y agoIf you ask kids how old they are, you will often get "5 and a half" etc. They are, correctly, thinking of time as a continuous variable. You "turn 1" when your age reaches 1.0.
- mc32 6y agoFor ages people use the unit that best captures the spirit of what they want to communicate. So they may say one hour old, five days old, six months old, etc. a fifty year old typically doesn’t say they are a half centenarian either. Now decades are used to express an age range like so and so is a nonagenarian to avoid being specific.
- coldtea 6y agoYou didn't have 1 year, but you had 1 day old or 2 weeks old, or 3-6 months old, etc. People never said and still dont' say "this baby is 0 years old". They start at the first complete item of the lower level time units and progress from there...
- gjm11 6y agoIntuition depends on what you're used to. The ancient Greeks didn't think 1 was a number; as late as ~1500CE Isidore of Seville was saying "one is the seed of number but not number". After all, if there's only one thing you don't need to think of it as a set of things, so there's no counting to do. (I think in some quarters it was even argued that two isn't a number, but I forget the details. Compare the fact that some languages treat pairs of things differently in their syntax from larger groups.) But it turns out that mathematically it's a really good idea to count 1 as a number and allow sets of size 1, and these days everyone does it and no one is very confused by it. (Well, maybe by the "sets of size 1" thing. Exhibit A: Perl. Exhibit B: Matlab.) And because we're now all used to thinking of 1 as a number, it's hard even to understand how anyone might have found it unintuitive. If we all started counting from zero and taught our children to do likewise, intuitions 50 years from now might be somewhat different from what they are now.
- userbinator 6y agoThe main argument I have against 1-based indexing is that it makes a lot of indexing calculations, which all programmers will need to learn at some point, even more confusing with all the additional +/-1. In other words, 1-based is easier only for the easy cases, but harder for the hard cases. In some ways I think it's similar to the endianness debate: big endian looks natural at first glance, but when you see the expression for the total value, having the place value increase with the address just makes far more sense since it eliminates the extra length-dependent term and subtraction.
- moocowtruck 6y agoi have two daughters learning python right now and they did not have an issue with 0 based and i only explained it once.. So i don't think know if i'd say your experience is concensus
- heavenlyblue 6y agoThere's a huge difference between a teaching language for _kids_ and _adults_. That's pretty much the case for most of the skills you would ever teach, not just computer languages.
- liveoneggs 6y agolike all of the special magic strings? the bloated syntax? the impossible (for professional adults!) environmental and ecosystem issues?
- aden1ne 6y agoI've always found the English language to simply be lacking here. In for instance Dutch there is a very clear distinction between half-open and closed intervals: "x tot y" is always half-open, "x tot en met y" is always closed. Whereas in English, "x until y" can mean both things, with all the resulting misunderstandings.
- Izkata 6y agoWe have "to" and "through" in English to distinguish them. Unfortunately "to" isn't always as strict as I'd like... "Until" I'd think of as being the strict "to", except something about it feels off like it couldn't just be dropped in every place to/through can be used.
- Jtsummers 6y agoWhile it isn't used often in conversational English, Common Lisp has a nice way of describing different ranges: for x from 0 to 10 ;; closed, [0,10] for y from 0 below 10 ;; (half-open, [0,10) "below" is unambiguous, as 10 is not below 10.
- janderson3 6y agoInteresting. Python is my go to teaching language. It beat the snot out of learning programming with Java. As a newbie, what does "public static void main" mean? So what's your choice for a teaching language? Maybe Lua or Scheme? They seem to have few surprises which is good for teaching.
- rockostrich 6y agoKotlin or Scala if you want Java without the verbosity of Java?
- dTal 6y agoBASIC, for explaining the fundamentals of imperative programming. Its weakness as a "real" language is its strength as a training language. Then Scheme, once they feel straightjacketed with BASIC; it will teach them what abstraction is and how to break code into chunks. It should be a bit of a lightbulb moment. They also get a type system. Finally, Julia, once they get sick of the parentheses and difficulty of building "real" programs (you'll do a lot of DIY FFI to use standard libraries in Scheme). From this they will also learn array programming and the idea of a type hierarchy. The only trouble with this curriculum is the student will be completely ruined for ever programming in C or Java. They will not have learned to tolerate the requisite level of tedium, or even programming without a REPL.
- fpoling 6y agoAnother thing is that in C null pointer is invalid. Yet the index zero is valid. With one-based indexes this discrepancy is removed.
- deleted 6y ago[deleted]
- antpls 6y agoMy own experience disagrees with your anecdote, but I guess it's different for everyone. I learned programming by myself when I was a teenager, and 0-indexed never was an issue. We learn very early on as kids at school that there are 10 digit numbers, not 9. You learn to write 0 before any other characters. On every digit pad, such as phone dial, there is a "0". If I ask you the list of digit numbers, 0 is the first element. There is nothing counter-intuitive about that in my opinion, even for a kid.
- sli 6y agoI think it's unintuitive because it's taught in that unintuitive way of just saying "zero is first." I know we can't get too terribly deep into this stuff with esp. young children, but we can at least gently introduce them to the concept of offset v. ordinal, as the former is a much more intuitive way to actually understand array indexes. Trying to liken indexes to ordinal numbers just promotes rote memorization, which is already time which would be better spent on other things.
- spiffytech 6y agoI've come to feel that "arrays are pointers with offsets" is a largely-irrelevant justification for promoting zero-based indexes in today's programming landscape. Looking at the TIOBE top 10 programming languages for this month, 6 of 10 languages don't even offer a way to address memory with numeric pointers (and that climbs to 8/10 if you rule .NET as "pointers are technically available, but not commonly used and are somewhat discouraged"). If I grab JS/Java/Python/PHP, an array/list is a magic sequence of values with opaque under-the-hood operations, and my only interface to it is the item index. In that context, promoting idioms because they make mechanical sense in a language I'm not using doesn't seem compelling.
- kazinator 6y ago> My son is doing Python at school, and the "first-element-is-actually-element-zero" is something that has to be dinned into them Maybe kids don't use rulers now? Just tell them that arrays are like rulers.