       Re: understanding code

• To: mathgroup at smc.vnet.net
• Subject: [mg114196] Re: understanding code
• From: Leonid Shifrin <lshifr at gmail.com>
• Date: Fri, 26 Nov 2010 05:31:44 -0500 (EST)

```Sam,

i is in this context what is called "indexed variable". It can be used in
most cases where the variable can,
except in some where Symbol is required explicitly, such as a part
assignment or local variables in scoping
constructs or pattern names etc. The part assignment restriction is
illustrated by the following:

In:= a={1,2,3}
Out= {1,2,3}

In:= a[]
Out= 2

In:= a[]=0
During evaluation of In:= Set::setps: a in the part assignment is not
a symbol. >>
Out= 0

Similarly with other cases where Symbol is required. I briefly discussed
indexed variables here:

http://www.mathprogramming-intro.org/book/node91.html

In this particular case, however, since there is i[_] as well, you can also
interpret i as a function,
defined both in general and on some specific values of the argument. The
difference between
functions (pattern-based) and variables is somewhat blurred in Mathematica.

Hope this helps.

Regards,
Leonid

On Thu, Nov 25, 2010 at 1:57 PM, Sam Takoy <sam.takoy at yahoo.com> wrote:

> Hi,
>
> The following code:
>
> i[_] = 0;
> i++
> i++
> i++
> i++
>
>
> 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? Is it a function or a
> value? Etc...
>
>
> Many thanks in advance,
>
> Sam
>
>

```

• Prev by Date: Re: disappointing CUDA speed
• Next by Date: Finding local maxima in multidimensional array (efficiently)
• Previous by thread: Re: understanding code
• Next by thread: Re: understanding code