Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: understanding code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114220] Re: understanding code
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sat, 27 Nov 2010 03:38:32 -0500 (EST)

Sorry, I meant PostIncrement (although similar arguments should apply to
PreIncrement as well)


On Fri, Nov 26, 2010 at 1:26 PM, Bill Rowe <readnews at sbcglobal.net> wrote:

> On 11/25/10 at 5:57 AM, sam.takoy at yahoo.com (Sam Takoy) wrote:
>
> >The following code:
>
> >i[_] = 0;
> >i[1]++
> >i[1]++
> >i[1]++
> >i[1]++
>
> >return 0 1 2 3 4. I'd like to understand whats going on here. That
> >is,
>
> >What is i?  Is it a function? And what is i[1]? Is it a function or
> >a value? Etc...
>
> It is a function. i[1] is the function i evaluated at i. You can
> see what is going on by using Trace, i.e.,
>
> In[3]:= Trace[i[1]++]
>
> Out[3]= {i(1)++,{i(1),0},{i(1)=1,1},0}
>
> As you can see, i[1] gets evaluated to whatever value it had
> previously which initially is zero. Then the result gets
> incremented by 1 and is then assigned to be the new value for
> i[1]. This last step is not something I would have expected
> without your example and I don't know why it occurs.
>
> I can see times where this behavior might be useful. For
> example, it is at times convenient to see i[1] as a subscripted
> variable. Thinking of i[1] this way, the notation i[1]++ would
> be interpreted as incrementing the subscripted variable by one
> and the Mathematica behavior is consistent with this.
>
>
>


  • Prev by Date: Re: Documentation of Eigenvalues[], Eigensystem[]
  • Next by Date: Re: disappointing CUDA speed
  • Previous by thread: Re: understanding code
  • Next by thread: Re: understanding code