4

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.

vapor
  • 7,911
  • 2
  • 22
  • 55
  • @Kuba what if I want to avoid the use of ToExpression – vapor Jun 02 '16 at 07:33
  • 1
    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"] – Kuba Jun 02 '16 at 07:36
  • @Kuba I am also curious if there is any method that can temporarily suspend the OwnValues of that symbol – vapor Jun 02 '16 at 07:38
  • @Kuba yes, I ended up with the same problem with Block – vapor Jun 02 '16 at 07:41
  • 1
    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. – Kuba Jun 02 '16 at 07:47
  • @Kuba I see, it seems I don't have a correct knowledge of the convention when to use ToExpression. I will think about it. – vapor Jun 02 '16 at 07:49
  • I'm encouraging you to not trust me in 100% :) I'm learning mma each day, even basic things. ps. why do you want to avoid ToExpression? – Kuba Jun 02 '16 at 07:50
  • @Kuba Thank you very much for your patience. When I do code generation, I see people prefer to play a lot with evaluation control and tricks, but I see no experienced programmer write ToExpression code like what I wrote in the question, though it is easier to understand/write and it works. I believe there must be some other reason than it's ugly(but I don't know the reason, so I simply avoided the use of ToExpression as I can). – vapor Jun 02 '16 at 07:54
  • I suppose the it's not ToExpression but a working with Strings what is not popular. Not only it's ugly but "x" misses current $Context for example. – Kuba Jun 02 '16 at 08:02
  • This is the oldest question on Stack Exchange I know that relates to this problem, and Leonid's answer serves as the foundation for my own methods below. – Mr.Wizard Jun 02 '16 at 08:08
  • Just for record,my[a_,b_]:=ToExpression[TemplateApply["Set[``,``]",{a,b}]].Of course,you don't like ToExpression. :) – yode Jun 15 '16 at 05:44

1 Answers1

3

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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Wow this is really clever! – vapor Jun 02 '16 at 07:57
  • While MakeExpression is not exactly ToExpression I feel like it's the same in this context. Nontheless P function is mindblowing! – Kuba Jun 02 '16 at 08:09
  • @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? – Mr.Wizard Jun 02 '16 at 08:11
  • @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 :) – Kuba Jun 02 '16 at 08:15
  • @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.) – Mr.Wizard Jun 02 '16 at 08:19