3 ms·
>Do all my arrays have the same origin? When I was learning Haskell, I actually was able to answer no to this question. The Array [0] data type in Haskell lets
by gizmo686 9y ago
>Do all my arrays have the same origin?
When I was learning Haskell, I actually was able to answer no to this question. The Array [0] data type in Haskell lets (requires) you to specify the bounds (upper and lower) when you create a new Array, and I ended up mixing 0 indexed and 1 indexed arrays throughout my program.
[0] Which you should almost never use. Use Data.Vector or Data.Sequence instead.
- ktRolster 9y agoInterestingly, you can do the same in C. For example: int *x = malloc(sizeof(int)*100); x = x + 50; Then you can use negative indexes, and this is actually valid code: x[-10] = 1337; I've only had use for that one time, when I was doing some kind of graph processing and it was convenient/efficient to have the origin in the middle of the array (instead of translating on each access).
- tacostakohashi 9y agoOr even -10[x], if you're really into using a silly index.
- Iv 9y agoI read that with a combination of fascination and nausea. Well played. But actually I remember that it can occasionally be used in image processing too, when you would use a pointer to iterate through an image, I would routinely use things like dx = *(ptr+1)-*(ptr-1); which is essentially the same as dx = ptr[1] - ptr[-1];
- MichaelBurge 9y agoIn Perl, just set "$[ = 17;" to make all of your arrays 17-indexed: http://search.cpan.org/dist/perl-5.19.0/ext/arybase/arybase.pm http://search.cpan.org/dist/perl-5.19.0/ext/arybase/arybase....
- c0nfused 9y agoThis is the sort of language feature we should all be implementing. I can imagine myself hunched over my keyboard at 3am swearing quietly and trying to figure out why the hell this is happening to me right now.