MathGroup Archive 2010

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

Search the Archive

Re: understanding code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114194] Re: understanding code
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Fri, 26 Nov 2010 05:31:23 -0500 (EST)
  • References: <iclfeu$lbj$1@smc.vnet.net>

Am 25.11.2010 11:57, schrieb Sam Takoy:
> Hi,
> 
> 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? 

it is a symbol. Depending on what kind of global definitions
(substitution rules) you define for i, it will rather behave as a
variable or a function or ...

> And what is i[1]? Is it a function or a value? Etc...

i[1] is an expression, as everything in Mathematica :-). Depending on
whether you have defined DownValues for i and what kind of DownValues
you have defined it will evaluate to a new expression. If it is part of
a more complex expression also the UpValues or SubValues might influence
the evaluation.

I think the mystery here lies in the ++ which is short for Increment.
Increment has the Attribute HoldFirst, which changes the standard
evaluation procedure in such a way that Increment sees the unevaluated
expression that it gets as the first argument. It is then the code in
Increment that obviously is smart enough to handle the i[1] in a way
that one most probably wants when using i[1]++ (or what the responsible
WRI developer thought one would expect). I think it is not something
that is built into the Mathematica language (other than that Increment
is a system function) and could be understood by explaining more about
what i[1] actually is.

hth,

albert




  • Prev by Date: Re: Efficient search for bounding list elements
  • Next by Date: Re: Why does this pattern fail to match?
  • Previous by thread: Re: understanding code
  • Next by thread: Re: understanding code