2
$\begingroup$

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?

$\endgroup$
6
  • 4
    $\begingroup$ Try SetAttributes[SetParameters, HoldAll]; SetParameters[rulelist : {__Rule}] := Set @@@ Unevaluated[rulelist] and report back. $\endgroup$ Jan 23, 2021 at 19:29
  • $\begingroup$ @J.M. Yep, that seems to work. Very simple, thanks! $\endgroup$
    – Chris K
    Jan 23, 2021 at 19:36
  • $\begingroup$ @J.M. BTW, I figured out that you can suppress the output by wrapping the right-hand side in (). $\endgroup$
    – Chris K
    Jan 23, 2021 at 19:40
  • $\begingroup$ @J.M. 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? $\endgroup$
    – Chris K
    May 2, 2021 at 19:26
  • $\begingroup$ That sounds like a good extension to this question. Since it hasn't been answered yet, it'd be kosher to edit your question with the additional stipulation. $\endgroup$ Jun 10, 2021 at 1:30

1 Answer 1

1
$\begingroup$

I'd use Mr.Wizard's step[] function for the most generality.

(* https://mathematica.stackexchange.com/a/1447 *)
step // ClearAll;
SetAttributes[step, HoldAll]
step[expr_] := Module[{P}, P = (P = Return[#, TraceScan] &) &;
   TraceScan[P, expr, TraceDepth -> 1]];

setParameters // ClearAll;
SetAttributes[setParameters, HoldAll];
setParameters[rulelist : {__Rule}] :=
    Hold[rulelist;] /. Rule -> Set // ReleaseHold;
setParameters[rulelist_] := With[{r = step[rulelist]},
   setParameters @@ r /; Head[r] === HoldForm];

Examples:

Clear[x, y];
rl = {x -> 1, y -> 2};
setParameters[rl]
{x, y}
(*  {1, 2}  *)

Clear[x, y, foo];
x = 1; y = 2;
foo[a_, b_] := {x -> a, y -> b};
setParameters[foo[11, 22]]
{x, y}
(*  {11, 22}  *)

Clear[nada, rien];
nada = rien;
setParameters[nada]
(*  setParameters[rien]  *)
$\endgroup$
4
  • $\begingroup$ An alternative to step[], but not as general: setParameters[rulelist_Symbol] := With[{r = Hold[rulelist] /. OwnValues[rulelist]}, setParameters @@ r /; Hold[rulelist] =!= r]; $\endgroup$
    – Michael E2
    Jun 10, 2021 at 4:00
  • $\begingroup$ I can't get the alternative in your comment to work (just returns unevaluated) but the main answer in the text seems perfect. Thanks! $\endgroup$
    – Chris K
    Jun 11, 2021 at 19:47
  • $\begingroup$ @ChrisK It should work for rl but not for foo[11, 12]. That's the "not as general" restriction. You also need the setParameters[rulelist : {__Rule}] := Hold[rulelist;] /. Rule -> Set // ReleaseHold; def. to go with the OwnValues one in my first comment. $\endgroup$
    – Michael E2
    Jun 11, 2021 at 20:00
  • $\begingroup$ Got it, that was the part I was missing. $\endgroup$
    – Chris K
    Jun 11, 2021 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.