Re: How to produce variable substitution list?
- To: mathgroup at smc.vnet.net
- Subject: [mg90929] Re: How to produce variable substitution list?
- From: Stonewall Ballard <stoney.nospam at sb.org>
- Date: Wed, 30 Jul 2008 03:52:02 -0400 (EDT)
- References: <g6mat3$i19$1@smc.vnet.net> <g6mqbq$nu3$1@smc.vnet.net>
On 2008-07-29 06:11:06 -0400, Valeri Astanoff <astanoff at gmail.com> said: > On 29 juil, 07:47, Stoney Ballard <ston... at gmail.com> wrote: >> Is there a straight-forward way to write a function that takes a list >> of symbols that are bound in the calling environment and produce a >> list such as is produced by Solve, like {var1->val1, var2->val2,...}? >> >> Currently, I'm using an expression (not a function) like {Hold[var1]-> var= > 1, Hold[var2]->var2}, which produces the right result, but it >> >> looks bad to duplicate the symbols. I want something that looks clean, >> like bindings[var1, var2,...]. >> >> I know this isn't lisp, and there's no binding environment as such, >> but it would expect this to be doable. >> >> TIA for any suggestions. >> >> - Stoney > > Good day, > > This is a solution inspired from Roman Maeder's > "Programming in Mathematica": > > In[1]:= SetAttributes[myRules,HoldAll]; > myRules[vars_List]:= > With[{vv=Map[Hold,MapAt[Hold,Hold[vars],{1,0}],{2}][[1]]}, > List@@(# -> ReleaseHold[#]& /@ vv) > ]; > > In[2]:= var1=3; var2=5; > myRules[{var1,var2}] > > Out[4]= {Hold[var1]->3,Hold[var2]->5} > > > V.Astanoff It's too bad the quoted-printable conversion made your code difficult to read in the newsgroup. Once I realized that "With" ignored "Hold", your solution worked fine. While investigating it, I came up with this simplified form, which seems to produce the same result: myRules[vars_List] := Thread[Rule[Map[Hold, Hold[vars], {2}][[1]], vars]] Thanks for your help. - Stoney