3
$\begingroup$

I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g.

x = 1;
y = 2;
VariablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> z} *)

Is this even possible?

$\endgroup$
  • 1
    $\begingroup$ This is a super interesting question. This post is related, because you will need to get the symbol names in order to Clear them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name. $\endgroup$ – march Feb 6 at 20:34
  • 1
    $\begingroup$ Is OwnValues /@ Unevaluated@{x, y, z} OK? $\endgroup$ – xzczd Feb 7 at 3:28
4
$\begingroup$

Update 2

Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:

SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var

and according to my original interpretation of the problem:

SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]

Update 1

After some comments from the OP, it seems they want instead something like

variableToRule[var_] := SymbolName@Unevaluated@var -> var

instead.

Original Post

Here's a first iteration. First define the helper function,

ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
 , Clear@var
 ; var -> val
 ]

Then, the function is

ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars

This uses the trick from this answer.

Then,

x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
$\endgroup$
  • $\begingroup$ Thanks - any way to keep the variables' values afterwards? $\endgroup$ – Chris K Feb 6 at 20:57
  • $\begingroup$ @ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values of x and y, etc. in expressions that contain those variables? In that place, you don't want x and y set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get {1 -> 1, 2 -> 2}. $\endgroup$ – march Feb 6 at 21:02
  • $\begingroup$ I think the trick from your link works: variableToRule[var_] := SymbolName[Unevaluated@var] -> var seems OK $\endgroup$ – Chris K Feb 6 at 21:02
  • $\begingroup$ But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself. $\endgroup$ – march Feb 6 at 21:04
  • $\begingroup$ It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks! $\endgroup$ – Chris K Feb 6 at 21:07
4
$\begingroup$

Pass in the names of the symbols as strings, and the rest is quite easy:

ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
    s // Apply[ClearAll];
    MapThread[Rule, {Symbol /@ s, t}]
];

ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
$\endgroup$
  • $\begingroup$ Thanks - any way to keep the variables' values afterwards? $\endgroup$ – Chris K Feb 6 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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