How to Explain This Behavior of Simple Procedures?
- To: mathgroup at smc.vnet.net
- Subject: [mg109612] How to Explain This Behavior of Simple Procedures?
- From: James Stein <mathgroup at stein.org>
- Date: Sat, 8 May 2010 07:06:33 -0400 (EDT)
I think the scanty procedures below show unexpected, perhaps buggy,
behavior.
These two procedures work as expected:
f assigns a value to a variable passed in by g:
*Input:*
ClearAll [f, g];
f [lhs_, rhs_] := lhs = rhs;
g[rhs_] := Module[{localVar},
f [localVar, rhs];
Print["g: localVar=", localVar];
];
g[44];
*(printed) Output:*
g: localVar= 44
But this method, where f receives the variable differently, fails:
*Input:*
ClearAll [f, g];
f [lhs_List, rhs_] := lhs[[1]] = rhs;
g[rhs_] := Module[{localVar},
f [{localVar}, rhs];
Print["g: localVar=", localVar];
];
g[44];
*(printed) Output:*
g: localVar=localVar$313085
In addition, there is an error message:
Set::setps :
(localVar$313085} in the part assignment is not a symbol.
which is curious because:
Head[localVar$313085] returns Symbol.
My motivation here is to allow a procedure to return one or more subsidiary
results in variables provided by the caller.