[Submission] D Slices

Mafi mafi at example.org
Tue May 31 10:22:44 PDT 2011


Am 31.05.2011 18:10, schrieb eles:
>> I don't think you can enter this debate without bringing the other
> war
>> about zero-based indices vs. one-based indices. Matlab first's index
>> number is number one, and I think this fits very naturally with the
>> closed-right limit. In many fields, one-based indices are common
>> because they are easier to reason with. But in computers, where the
>> index is generally an offset from a base address, zero-based is much
>> more prevalent as it better represents what the machine is actually
>> doing. And it follows that open-ended limits on the right are the
> norm.
>
> Actually, I think 0-based vs. 1-based is of little importance in the
> fact if limits should be closed or open. Why do you think the
> contrary?
> Why it is "natural" to use open-limit for 0-based? I think it is
> quite subjective. Why to the right and not to the left?
> Can you redirect me towards an example of such norm? I have no
> knowledge of that use.
> I hope some day someone would not have to write a paper like http://
> drdobbs.com/blogs/cpp/228701625 but targetting... D's biggest mistake
> was to use open-limit on the right.
> I think the idea of dropping array information to just a pointer was
> seen as very natural to Kernigan and Ritchie. Otherwise they would
> have not used it.
>

In my opinion it is natuaral to use half open intervals for zero-based 
indices. My reasoning:

//zero-based
int[8] zero; //indices from 0 upto and excluding 8 -> [0,8)

//one-based
int[8] one;
//in many languages the indices from 1 upto and including 8 -> [1,8]

Then, using the same type of inetrval seems natural for slicing.

Mafi


More information about the Digitalmars-d mailing list