Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure it does. -2 gives second to last, -3 gives third to last, etc. If you only use negative indices i, the "base" of the array is the end, and arr[i] is just arr[len(arr)+i].


How does that fall within the definition of "base index"? that's just array position referencing.

You can't even create an array/list in python w/o a hard 0 reference.


First, it's a "joke", second, nobody is going to force you to use non-negative indices. Sure, you can use a base index of zero and read left to right... but you can use a base index of -1 and read right to left and live a full (if lonely) life without considering the passé alternatives.

More precisely, I'm defining the "base" to be the right hand side of the array and operating on the reversed list -- it's just a change of variables. Python supports -1 based indexing, but does not support 1 based indexing. You could easily define a language which only provides the -1 based indexing but does not support 0-based. Let me demonstrate:

  void *malloc_r(int size, int count) {
    void *m = malloc(size, count+1;
    m[count] = count;
    return m+count;
  }

  void free_r(void *m) {
    free(m - m[0]);
  }
Memory allocated through malloc_r is right-aligned and you should only use negative indices. Best not touch m[0]!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: