From: James Stein on
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)


From: David Bailey on
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

From: John Fultz on
The problem isn't in Set, it's in SelectionEvaluate. SelectionEvaluate doesn't
do what you think it does. You think it's evaluating the selection immediately.
Instead, it's queuing the selection to be evaluated, and the actual evaluation
occurs after the present evaluation of cell4 is finished.

There are various ways to get the behavior you want, but none particularly easy.
I hesitate to recommend anything specific without having a better idea of what
the big picture is of the thing you're trying to accomplish.

Sincerely,

John Fultz
jfultz(a)wolfram.com
User Interface Group
Wolfram Research, Inc.

On Wed, 7 Jul 2010 07:43:12 -0400 (EDT), 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)



From: James Stein on
Dear David and John,

Thanks for your explanations. I remember now what I was trying to do with
SelectionEvaluate. It was something like this (I simplify the situation):

Suppose 'g' is a deeply recursive function, defined something like this (to
make calculations faster by preventing re-valuation with identical
arguments,):


g [ n_Integer, a_List ] :== g [ n, a ] == Module [ (* =85 *) ] ;


I discovered that I often wanted to be able to clear currently retained
computed values of g, then compute another value of g, without having to
evaluate (click in) more than one cell. Placing the evaluation of g in the
same cell where g was defined was a bad choice during debugging.


Looking for a solution, it appeared that 'EvaluateSelection' might work. In
retrospect, I could have simply changed the second g to an f:


g [ n_Integer, a_List ] :== f [ n, a ] == Module [ (* =85 *) ] ;


Then something like this, in a single cell, works:

Clear [ f ]

g [ 3, { 1, 2, 3 } ]

Clear [ f ]

g [ 4, { 5, 3, 12} ]