I'd a like a function that assigns variables based on a rule list. That is,
SetParameters[{x->1, y->2}]
would set x=1 and y=2. The problem is when x and y already have values, this understandably leads to Set::setraw: Cannot assign to raw object 1. errors. Is there a way around this using non-standard evaluation?
My attempt so far, which doesn't work:
Clear[SetParameters];
SetAttributes[SetParameters, HoldAll];
SetParameters[rulelist_] := Module[{vars},
vars = Unevaluated[rulelist][[All, 1]];
Print[vars];
Set[Evaluate@vars, rulelist[[All, 2]]];
]
Additional wrinkle:
@J.M.'s suggestion in the comments answered my original question, but afterwards I uncovered a complication: What if I want to define the rulelist ahead of time? E.g., rl = {x->1, y->2}; SetParameters[rl] Any way to make that work?
SetAttributes[SetParameters, HoldAll]; SetParameters[rulelist : {__Rule}] := Set @@@ Unevaluated[rulelist]and report back. – J. M.'s missing motivation Jan 23 '21 at 19:29(). – Chris K Jan 23 '21 at 19:40rl = {x->1, y->2}; SetParameters[rl]Any way to make that work? – Chris K May 02 '21 at 19:26