4 ms·
A lot of mathematics (and real life, but lets not go there) is written 1-based, which makes it quite a pleasure to translate algorithms into Julia. But I don't
by idunning 13y ago
A lot of mathematics (and real life, but lets not go there) is written 1-based, which makes it quite a pleasure to translate algorithms into Julia. But I don't think its a big criticism OR selling point really - sometimes its good, sometimes its bad. I personally enjoy it, and I make less indexing errors than I do when writing array/matrix intensive code in C++.
- moron4hire 13y ago"This is the way we've always done it" is not a convincing argument. Dijkstra makes all of the convincing arguments for why 0-based is better than 1-based. I haven't made an indexing-based error in over 10 years, and the only reason I ever had it before was from having to switch back and forth between C and VB6 years ago.
- tristanz 13y agoI'm guessing you don't translate algorithms in scientific papers. The reality is that having one notation for mathematics and programming is a convincing argument to many. More broadly, this indexing thing is trivial. Let's not pretend Dijkstra's paper is proof of anything. At the end of the day his argument is just style: "That is ugly, so for the upper bound we prefer < as in a) and d)" http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831....
- simonster 13y agoOne counterargument is that in natural language, we tend to talk about numbers 2 to 12 and not the interval 2 <= x < 13. The argument for the latter versus the former is probably Dijkstra's weakest: the claim is merely that this makes the empty range "unnatural." The former convention implies one-based indexing for the same reason that the the latter implies zero-based indexing. In Julia, the natural numbers 2, 3, ..., 12 are expressed as 2:12 and the empty range starting at 2 is expressed as 2:1.
- tristanz 13y agoThis is also typically written in algorithms like: N = rows(data) for i in 1:N blah So even though 1:0 may seem not unnatural, it actually reads perfectly in most code.
- Fomite 13y agoFor me, the biggest positive is avoiding indexing errors between 0-indexed languages (Python) and 1-indexed data (almost everything I get). Patient IDs, simulation code, etc. hardly ever starts with "0" as the first ID unless it's been created by a computer scientist, and having to make sure everything is looking at ID-1 instead of ID is a pain. And makes code less useful to people who use the code instead of making it.
- john_b 13y ago> "This is the way we've always done it" is not a convincing argument. You're fighting a straw man; that isn't the argument. 1-based indexing is elegant for many mathematical uses. Technical computing often implements algorithms that are best written down using mathematical notation. In such a case the largest conceptual difficulty is not the origin of the index but the successful translation of the algorithm. To minimize the possibility of error, the original indexing (especially when it involves nontrivial mathematical maps) is often preserved. > Dijkstra makes all of the convincing arguments for why 0-based is better than 1-based. He essentially makes only one point. Namely that, when using 0-based indexing we can easily determine the length of a sequence by only its upper index. Though it's not a profound observation since the index bias is zero, it often gets treated as such. Either way, it's a convention. A competent programmer should be able to handle any well-specified indexing convention (including those starting at negative indices). Different circumstances will confer different benefits on different conventions, and you should use the one that's most appropriate for the task at hand rather than religiously promoting The One True Way (TM).
- bjourne 13y agoThe big drawback is that with 0-based indexing you can reference elements relative to the tail. If 0 is the first index and -1 the last index, then 1 is the second index, -2 the second last, 2 the third, -3 the third last and so on. 1..-1 would be all elements from the second up to including the second last. I don't see how you could have indexing from the tail being internally consistent if the arrays are 1-indexed.
- astrieanna 13y agoJulia uses the keyword `end` to indicate the last index. 1 and end for the first and last, 2 and end-1 for the second and second to last, and so on.