MathGroup Archive 2010

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

Search the Archive

Can This be Possible? A bug in Set (=) ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110806] Can This be Possible? A bug in Set (=) ?
  • From: James Stein <mathgroup at stein.org>
  • Date: Wed, 7 Jul 2010 07:43:12 -0400 (EDT)

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)



  • Prev by Date: Re: Change Default Button Method
  • Next by Date: Re: overloading a function name in a package? How to query all names?
  • Previous by thread: Re: PlotLegend-> and DateListPlot[]...incompatible or
  • Next by thread: Re: Can This be Possible? A bug in Set (=) ?