I was surprised by the following sequence of expressions:
Remove[foo$, bar$];
foo$ = Function[Set[bar$, 42]];
At this point, I expected foo$ to be defined and bar$ not to be defined, because I had not called the function named foo$ yet. Indeed, the notebook interface shows bar$ in blue (undefined) and foo$ in black (defined). However, bad news, bar$ has a value:
foo$
(* (bar$ = 42) & *)
bar$
(* 42 *)
Running the command-line version of Mathematica produces different results:
Remove[foo$, bar$]
foo$ = Function[bar$ = 42];
foo$
(* (bar$ = 42) & *)
bar$
(* bar$ *)
The following does not help; in the notebook:
Remove[foo$, bar$]
bar$
(* bar$ *)
foo$ = Function[SetDelayed[bar$, 42]]
(* (bar$ := 42) & *)
bar$
(* 42 *)
and in the command-line:
Remove[foo$, bar$]
foo$ = Function[SetDelayed[bar$, 42]]
(* (bar$ := 42) & *)
bar$
(* bar$ *)
I found only the following to work, but it's unacceptable because I want a functional way to delay and to force evaluation, that is, a method that uses the construction and calling of functions:
In the notebook:
Remove[foo$, bar$]
foo$ = Hold[Set[bar$, 42]]
(* Hold[bar$ = 42] *)
bar$
(* bar$ *)
ReleaseHold[foo$]
(* 42 *)
bar$
(* 42 *)
and identical results in the command-line.
(x=0)&;andxin seperate cells the behavior is different if I evaluate the cells together vs. one at a time.. Looking forward to a good explination.. – george2079 Dec 24 '13 at 15:37DelayandForce. – Reb.Cabin Dec 25 '13 at 17:44