Re: Re: function mySet[]
- To: mathgroup at smc.vnet.net
- Subject: [mg36135] Re: [mg36121] Re: [mg36096] function mySet[]
- From: Hartmut Wolf <hartmut_lebrecht_wolf at yahoo.com>
- Date: Fri, 23 Aug 2002 00:25:07 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Omega Consulting To: mathgroup at smc.vnet.net [mailto:omega_consulting at yahoo.com] >Sent: Thursday, August 22, 2002 10:33 AM >Subject: [mg36135] [mg36121] Re: [mg36096] function mySet[] > > >At 04:52 AM 8/21/2002, Prof. Korvink wrote: >>I am struggling with a problem: >> >>I would like to have a function mySet[] that takes a string argument >>that is the correct string name of a "variable" and is able to >>make assignments to that variable. Naively: >> >>a = { 1, 2, 3, 4, 5 }; >>mySet[ s_String ] := >> Symbol[ s ] = { 2, 3, 4, 5, 6 }; (* ERRONEOUS! *) >>mySet[ SymbolName[ a ] ]; >>a >> >>should give: >> >>{ 2, 3, 4, 5, 6 } >> >>The problem is that Symbol[ s ] immediately evaluates to a List >>and I cannot for the love of anything generate an expression that >>evaluates to the actual unevaluated symbol. >>I have tried many variants with HoldAll and so on, with no luck, >>hopefully only out of ignorance. > >There is a much simpler way to do this if you want to use the >symbol. Just >have the function Hold to argument. > >Attributes[mySet] = {HoldFirst}; >mySet[s_Symbol] := (s = {2, 3, 4, 5, 6}) > >If you insist on using a string, then it can be done by using a >little-known form of ToExpression. > >mySet[s_String] := > ReleaseHold[ > Map[mySet, (* uses definition above *) > ToExpression[s, InputForm, Hold] > ] > ] > >-------------------------------------------------------------- >Omega Consulting >"The final answer to your Mathematica needs" > >Spend less time searching and more time finding. >http://www.wz.com/internet/Mathematica.html > > There is a fine point here as when to use which mySet. A drawback is, that you have to redefine one mySet before using the other. The question arises, how can we use that idea to makeit in one step. Here a suggestion: Clear[mySet] mySet[str_, expr_] := Block[{mySet}, Attributes[mySet] = HoldFirst; mySet[sym_] := (sym = expr); ReleaseHold[Map[mySet, ToExpression[str, InputForm, Hold]]]] so we have restricted scope for the inner mySet (and need not qualify the argument). You can (better) use a function instead (with Hold attribute): mySet[str_String, expr_] := ReleaseHold[ Map[Function[lhs, lhs = expr, {HoldFirst}], MakeExpression[str]]] This now is quite similar to a suggestion I had made earlier: mySet[str_String, expr_] := Function[lhs, lhs = expr, {HoldFirst}] @@ MakeExpression[str] With the Map idea we can, however, use a pure function without any Hold attribute: mySet[str_String, expr_] := ReleaseHold[Unevaluated[# = expr]] &[MakeExpression[str]] Here the fine point is in inserting Unevaluated, that delays the execution of Set after ReleaseHold has done its work -- Hartmut Wolf __________________________________________________ Do You Yahoo!? HotJobs - Search Thousands of New Jobs http://www.hotjobs.com