4
$\begingroup$

I want the function to take in string, then assign a value to its corresponding symbol. An implementation using ToExpression is

mySet[str_String, val_] := 
ToExpression["Set[" <> str <> "," <> ToString[val] <> "]"]

How can I do it without ToExpression? I wonder how this can be done with purely evaluation control.

I have read the following posts: Generating assignments and transforming code inside held forms when generating code

How do I evaluate only one step of an expression?

I think the main problem is to evaluate Symbol["x"] partially to x(it can have OwnValues), which can't be achieved with first post's code; and second post's code is indeed returning a HoldForm expression, leaving me generally the same problem.

$\endgroup$
11
  • $\begingroup$ @Kuba what if I want to avoid the use of ToExpression $\endgroup$ Commented Jun 2, 2016 at 7:33
  • 1
    $\begingroup$ I don;t think you can, since you are starting with a string and Symbol[] can't handle symbols with values: x = 5; Symbol["x"] $\endgroup$ Commented Jun 2, 2016 at 7:36
  • $\begingroup$ @Kuba I am also curious if there is any method that can temporarily suspend the OwnValues of that symbol $\endgroup$ Commented Jun 2, 2016 at 7:38
  • $\begingroup$ @Kuba yes, I ended up with the same problem with Block $\endgroup$ Commented Jun 2, 2016 at 7:41
  • 1
    $\begingroup$ The problem is, even if you want to avoid ToExpression during assignment you have to make MMA understand "x" is x which means you have to convert it to an expression (held or not) at some point, and whether you will use something different from ToExpression doesn't matter, you will effectively do this. E.g. you can export "x" as a text file and import/get as expression, but deeper this will still be ToExpression. $\endgroup$ Commented Jun 2, 2016 at 7:47

1 Answer 1

3
$\begingroup$

Since you referenced How do I evaluate only one step of an expression? I might use:

(* step loaded from referenced Q&A *)

mySet[str_String, val_] := step @ Symbol[str] /. _[s_Symbol] :> (s = val)

Now:

x = 5;

mySet["x", 7];

x
7

(This also makes use of Injecting a sequence of expressions into a held expression.)

For clarity the above is trying to answer in what I think is the spirit of your question.
Pragmatically I usually(1)(2)(3) use MakeExpression, see e.g. my answer to the closely related:

$\endgroup$
5
  • $\begingroup$ Wow this is really clever! $\endgroup$ Commented Jun 2, 2016 at 7:57
  • $\begingroup$ While MakeExpression is not exactly ToExpression I feel like it's the same in this context. Nontheless P function is mindblowing! $\endgroup$ Commented Jun 2, 2016 at 8:09
  • $\begingroup$ @Kuba That's why I did not use MakeExpression in my original answer, but I thought it best to include for reference, as well as your own lovely RawBoxes example. You are referring to the code of my step function? $\endgroup$ Commented Jun 2, 2016 at 8:11
  • $\begingroup$ @Mr.Wizard Yes, but I should have said mind-cracking or something because I still don't fully understand what's happening and how had you figured it out :) $\endgroup$ Commented Jun 2, 2016 at 8:15
  • $\begingroup$ @Kuba I will admit that despite its brevity that code took a lot of thinking on my part! The (P = (P = thing is self-redefinition a la (2676) to avoid spitting out the verbatim unevaluated expression. (By the way I just rewrote my answer to keep it more focused, yet hopefully reference what needs referencing for completeness. Let me know if you agree.) $\endgroup$ Commented Jun 2, 2016 at 8:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.