On Monday, 16 October 2017 at 18:56:03 UTC, H. S. Teoh wrote:

I suppose this is like Walter not daring to write code with 1-based arrays. :D On that note, though, in D I only rarely actually need to specify explicit indices. Foreach and generic code alleviates most cases of explicit indexing I'd normally write in C/C++ code, and I wouldn't be surprised if much of my code could actually be ported to 1-based indexing with only minor changes.

But I suppose if you're doing matrix/tensor math, you can't really avoid this.



I always thought slicing was a more important difference for 0 or 1-based indexing, rather than loops.

Consider D's A[m..n] vs. Matlab's A[m:n]. Matlab's is easier to explain to someone with a math background because you get A[m] through A[n, instead of A[m] through A[n-1] in Ds. However, D's A[m..n].length = n - m, whereas Matlab's length(A[m:n])= n - m + 1. Thus, you often find yourself adjusting formulae all over the place to account for the extra "+1". Nevertheless, if you have more of a mathematics background, you always need to remember that if you wanted A[m:n] in Matlab, you need A[(m - 1)..n] to get the equivalent in D.

Reply via email to