0

I would like to work with arrays (lists) whose first element has index 0 like arrays in C or Python. I cannot use Part[] since expr[[0]] is the head of expr and not its first element. I'm sure I'm not the only one to emulate C syntax in Mathematica. How can I do that?

gpap
  • 9,707
  • 3
  • 24
  • 66
coussin
  • 361
  • 1
  • 8
  • 8
    Mathematica expressions are indexed from 1. Index 0 gives you the head. This is not something that can be changed. For arrays, you can always have myPart[arr_, i_] := Part[arr, i+1], with additional definitions for other syntaxes of Part. But I still don't understand the real motivation behind wanting to do something like this. If you explain why you want this, maybe someone can come up with a better suggestion. – Szabolcs Oct 06 '15 at 09:08
  • Just for the ease of programming… Take CoefficientsList[]. Wouldn't it be nice if the index of this list was the power of x of the polynomials ? – coussin Oct 06 '15 at 10:08
  • I also like the Fortran way where you declare an array as i_start:i_end. This way, the index of the array has a direct meaning in the problem you code. – coussin Oct 06 '15 at 10:10
  • 3
    If the only reason is that you like it better, I think that trying to do this is a very bad idea. No matter what convention you use, 0-based or 1-based, there will be a problem for which it is not convenient. For that problem you'll just have to offset the indices. – Szabolcs Oct 06 '15 at 10:15
  • Starting at 0 is also inconsistent with default iterators in Mathematica, for instance in Table, Sum etc. It's much less hassle to just adapt ;) – Marius Ladegård Meyer Oct 06 '15 at 10:34
  • 1
    A big problem with manually hacking Part is essentially every System`* function implemented top level uses Part and thus you will most likely break them. – Greg Hurst Dec 03 '15 at 00:02

0 Answers0