Re: Can This be Possible? A bug in Set (=) ?
- To: mathgroup at smc.vnet.net
- Subject: [mg110828] Re: Can This be Possible? A bug in Set (=) ?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Thu, 8 Jul 2010 03:13:24 -0400 (EDT)
- References: <i11p5t$p2a$1@smc.vnet.net>
On 07/07/10 12:41, James Stein wrote:
> The definition of Set (=) is straight forward: "*lhs=rhs*
>
> evaluates *rhs* and assigns the result to be the value of *lhs*. From then
> on, *lhs* is replaced by *rhs* whenever it appears."
>
>
> How then to explain the output of cell 4 below, when cells 1 through 5 are
> evaluated in sequence (but not all at once, e.g. do NOT select all five
> cells and then press shift-return) ?
>
>
> (* cell 1 *)
> foo = 1;
> Print [ foo ]
>
>
> (* cell 2 *)
> foo = 2;
> Print [ foo ]
>
>
> (* cell 3 *)
> ClearAll [ evaluate, foo ];
> evaluate[n_Integer] := Module [ {nb, id, sel},
> Print [ "entering evaluate[", n, "], foo=", foo ];
> nb = EvaluationNotebook [ ];
> id = "cell "<> ToString [ n ];
> (* find desired string and select it: *)
> sel = NotebookFind [ nb, id ];
> If [ sel == $Failed, Return [ sel ] ];
> (* select entire cell containing string: *)
> SelectionMove [ nb, All, Cell ];
> (* set foo by evaluating selected cell: *)
> SelectionEvaluate [ nb ];
> (* verify[??] that foo has a new value *)
> Print [ "exiting evaluate[", n, "], foo=", foo ];
> Return [ foo ];
> ];
>
>
> (* cell 4 *)
> foo = 0;
> evaluate [1];
> foo
> evaluate [2];
> foo (* returns 0 *)
>
>
> (* cell 5 *)
> foo (* returns 2 *)
>
>
> If you enter the above, not all in a single cell but in five separate
> cells, then evaluate each cell in turn, you will discover that consecutive
> evaluations of 'foo' (the last two) return different values.
>
>
> How can this be? I was under the impression that Mathematica maintains a
> single symbol table. In this table will be an entry for "foo", with various
> associated definitions, values, syntax information, attributes, etc. So how
> is the following sequence be explained?
>
> Assign foo the value 2. (penultimate line in cell 4)
>
> Display foo (value is 0) (last line in cell 4)
>
> Display foo (value is 2) (last line in cell 5)
>
>
The crucial point here is that SelectionEvaluate *queues* the evaluation
to happen when the previous one is complete - it is not analogous to
calling a function. Although I have not worked through your example in
detail, I am pretty sure this is what is involved here.
SelectionEvaluate is a fairly specialised thing to use - do you have a
good reason to do so - for example a call to Switch to select among
various strings might solve your problem more elegantly (and much more
efficiently!).
David Bailey
http://www.dbaileyconsultancy.co.uk